字串常用操作

2022-08-09 17:06:14 字數 1738 閱讀 4523

1

常用:分割、長度、索引、切片

2 r = (1,2,3,4,5)#

唯讀列表元組

3 name = "

liangml"4

strip

5 username = input("

user:")

6if username.strip() == "

liangml

":#strip可以將輸入前後的空格都換掉

7print("

welcome")

89split

10 names = "

liangml

","tom

","jack

"11 name2 = names.split("

,")#

split是names拆分開來(得到乙個列表)

12print("

|".join(name2))#

join是將name2結果以|形式合併到一起

1314

print(""

in name)#

有沒有空格

1516

capitalize

17print(name.capitalize())#

capitalize是將名字的首字母大寫

1819

format20#

format字串兩種賦值方式

21 msg = "

hello,,it's been a long since last time sopke...."22

#msg2 = msg.format(name="zhangsan",age=111)23#

print(msg2)

24 msg2 = "

lisi,dddd"25

print(msg2.format("

liangml

",22))

2627

center

28print(name[2:4])

29print(name.center(40,"

-"))#

center可以指定長度,將字串兩邊的長度均勻的分開

3031

find

32print(name.find("

a"))#

find查詢賦值中有幾個包含字串

3334

isdigit

35 age = input("

your age:")

36if age.isdigit():#

isdigit判斷他是否為數字

37 age =int(age)

38else:39

print("

invalid data type")

4041

isalnum

42 name = "

liangml!dfs"43

print(name.isalnum())#

不容許複製裡面有特殊字元

4445

endswith、startswith

46print(name.endswith("

dfsd"))

47print(name.startswith("

dfsfds"))

4849

upper、lower

50print(name.upper().lower())#

upper賦值全部大寫,lower全部變小

字串常用操作。。。

include include include char itoa int value result char malloc sizeof char i 1 for j 0 jint i 65 char p itoa i printf s n p free p p null include incl...

字串常用操作

coding utf 8 import string1 判斷str unicode字串物件 def isastring anobj return isinstance anobj,basestring basesting是str,unicode的父類 defisaint anobj return i...

字串常用操作

一 變換大小寫 string.tolowercase string.touppercase var big qwertyu big.tolowercase qwertyu var yh qwertyui yh.touppercase qwertyui 二 獲取字串長度 var yy 好好學習前端做個...