python基礎 字串

2021-09-20 18:26:45 字數 1227 閱讀 9009

string = "hellow world"

print(string[0:5])

result = hello

string = "hellow world"

if "e" in string :

print("字母存在")

string = "hellow world"

print(string.upper())

result = "hellow world"

string = "hellow world"

print(string.lower())

result = "hellow world"

string = "hellow world"

print(string.islower())

result = false

string = "hellow world"

print(string.isupper())

result = false

isalpha():判斷字串全是字母,返回true

isalnum():判斷字串是字母或者數字,返回true

isdecima():判斷字串全是數字,返回true

isspace():判斷字串全是space, 返回true

istitle():判斷只有首字母大寫,返回true

​​​​​​​
list = ["red","blank","blue"]

color = ",".join(list)

print(color)

result = red,blank,blue

​​​​​​​
list = ["red","blank","blue"]

color = ",".join(list)

content = color.split(",")

print(content)

result = ['red', 'blank', 'blue']

​​​​​​​rjust(10)右邊對齊,不足是個的用空格填充 

python基礎 字串

轉義符 n換行 print 我是 nzzz 我是 zzz t製表符 print 我是 tzzz 我是 zzz 雙引號 print 我是 zzz 我是 zzz 單引號 print 我是 zzz 我是 zzz 續航符 name s z print name sz原始字串 原始字串 r abc r abc...

Python基礎字串

str1 hello python str1 str1.capitalize 把開頭轉換成大寫 print str1 str1 str1.center 8,居中並填充 print str1 str1 str1.find j 0,len str1 尋找元素所在的位置,沒在返回 1 print str1...

Python基礎 字串

判斷全部否是字母 str helloween if str.isalpha print 字母 判斷全部否是數字 str 1234 if str.isdecimal print 數字 判斷都是大寫 str abc if str.isupper print 大寫 判斷都是小寫 str abc if st...