字串型別

2021-10-19 08:02:12 字數 3656 閱讀 9133

#單引號注釋

a='hello ' \

'world'

print(a)

#雙引號注釋

a="hello world"

print(a)

# 三引號回車換行,不必新增任何的額外字元

d="""hello world"""

print(d)

#\在字串的前面加\屬於轉義字元,單引號不會搞混

c='i\'m erica'

print(c)

#字串的輸出

name="lixue"

print("我的名字叫 %s" % name)

print(f'我的名字叫')

#只要input輸入的資料型別都是字串型別

#下標從0開始計數

str1=("student")

print(str1[1])

# 切片

# 序列[開始位置下標:結束位置下標:步長]

name='abcdefg'

print(name[2:6:2]) #ce

print(name[2:5]) #cde

print(name[:]) # abcdefg

print(name[6:2:-1]) #gfed -1 代表從左到右。序列注意順序。如果和步長的方向相反,則無法選取資料

# find()如果子串存在返回的是下標。如果不存在返回的是-1

mystr=("qwer and school and student")

print(mystr.find('and',3,20)) #5

print(mystr.find('ands',5,10)) #-1

# index如果子串存在返回的是下標。如果不存在返回的是報錯

print(mystr.index('and',3,20)) #5

# print(mystr.index('ands',5,10)) #valueerror: substring not found

# count 返回的是出現的次數,如果不存在,返回0

print(mystr.count('and',3,20)) #2

print(mystr.count('ands',5,10)) #0

# rfind(),rindex從右側開始查詢。但是下標還是從左到右數的

mystr=("qwer and school and student")

print(mystr.rfind('and',3,20)) #16

print(mystr.rfind('ands',5,10)) #-1

print(mystr.rindex('and' )) #16

print(mystr.rindex('ands',5,10)) #-1

#----------說明我們的字串型別屬於不可變型別(原有的字串不會改變,修改的放在新的字串裡面)------------------------------#

# split():按照指定字元分割字串

# 會返回乙個列表,丟失分隔符號。

# 字串序列.split(分割字元,num) num是個數

mystr=("qwer and school and student")

list=mystr.split("and")

list=mystr.split("and",2)

print(list) #['qwer ', ' school ', ' student']

# .join()--合併列表裡面的字串資料為乙個大字串

# 字元或字串.join(多字串組成的序列)

mylist=['aa','bb','cc']

new_str='-666-'.join(mylist)

print(new_str)

# captialize():將字串第乙個字元轉換成大寫

#title():將字串每個單詞首字母轉換成大寫

#upper可以將字元所有小寫轉大寫,

#lower將字串所有大寫轉小寫

mystr='hello world'

print(mystr.capitalize())

print(mystr.title())

print(mystr.lower())

print(mystr.upper())

# lstrip():刪除字串左側空白字元

# rstrip():刪除字串右側空白字元

# strip()刪除字串兩側空白字元

my_str=' hao hao xue xi '

new_str=my_str.lstrip()

print(new_str)

my_str=(' hao hao xue xi ')

print(my_str.rstrip())

my_str=(' hao hao xue xi ')

print(my_str.strip())

# ljust():刪除字串左側空白字元

# rjust():刪除字串右側空白字元

# just()刪除字串兩側空白字元

my_str='hello'

new_str=my_str.ljust(11,',')

print(new_str)

new_str=my_str.rjust(11,',')

print(new_str)

new_str=my_str.center(11,',')

print(new_str)

# stratswith(子串,開始位置下標,結束位置下標) 判斷是否以這個字元開始的。是true

# endswith(子串,開始位置下標,結束位置下標) 判斷是否以這個字元結尾的 否 false

mystr='hello world my china'

print(mystr.startswith('hello'))

print(mystr.startswith('he'))

print(mystr.startswith('o',4,8))

print(mystr.endswith('a'))

print(mystr.endswith('h'))

print(mystr.endswith('n',1,19)) ###(按個數來的)

# isalpha() 字串是否都是字母

# isdigit() 字串是否都是數字

# isalnum() 所有字元都是字母或數字,true;若有其他空格或者標點false

# isspace():如果字串中只包含空白,返回true,否則false

my_str1="my god"

my_str2="137269"

my_str3="hello369"

my_str4=" "

print(my_str1.isalpha()) #因為有空格,所以不都是字母

print(my_str3.isalpha())

print(my_str3.isalnum())

print(my_str2.isalnum())

print(my_str2.isdigit())

print(my_str2.isspace())

print(my_str4.isspace()) #只包含! 沒有空格不行,有數字也不行

mysql字串型別 mysql 字串型別

字串型別 字串型別指char varchar binary varbinary blob text enum和set。該節描述了這些型別如何工作以及如何在查詢中使用這些型別。型別大小用途 char0 255位元組定長字串 varchar0 65535 位元組變長字串 tinyblob0 255位元組...

字串型別

c 定義了乙個內容豐富的抽象資料型別庫,其中,string和vector是兩種最重要的標準庫型別,前者支援可變長字串,後者則表示可變長的集合。還有一種標準庫型別是迭代器,它是string和vector的配套型別,常被用於訪問string中的字元或vector中的元素。本篇將介紹string型別。標註...

字串型別

redis字串型別鍵的查詢 1.get 查詢key的值 2.mget 查詢所有key的值 3.getrange 查詢乙個字串的子串,子串的內容取決於start和end.4.strlen 返回key的字串長度 5.getset 原子地給乙個key設定value並且將舊值返回 redis字串型別鍵的計數...