python 字串的基本操作

2022-06-13 05:33:09 字數 1636 閱讀 4160

name ='my \t is liufong'

print(name.capitalize())#將字串的第乙個字母變成大寫,其他字母變小寫

print(name.count('l'))#判斷是否存在

print(name.center(50,'-'))#不夠50個用-新增,元素放在最中間

print(name.endswith('ng'))#判斷元素是否存在

print(name.expandtabs(tabsize=30))#增加30個空格

print(name[name.find('name'):])#找到下標

print(name.format(name='liufong'))#賦值

print('12a'.isalnum())#判斷是不是阿拉伯數字

print('adc'.isalpha())#判斷是不是字母

print('1a'.isdigit())#判斷是不是整數

print('aa'.isidentifier())#判斷是不是合法的識別符號

print('aa'.islower())#檢測字串是否由小寫字母組成。

print('33'.isnumeric())#判斷是不是數字,純數字,不能有小數點

print(' '.isspace())#判斷是不是空格

print('my name is '.istitle())#判斷是不是空格

print('my name is '.isprintable())#判斷是不是可以列印 tty file,drive file

print('my name is '.isupper())#判斷是不是大寫

print('+ '.join('my name is'))#轉換成字串連線

print(name.ljust(50,'*'))#50個字串不夠從右邊填寫

print(name.rjust(50,'-'))#50個字串不夠從左邊填寫

print('sadad'.lower())#變成小寫字母

print('sadad'.upper())#變成大寫字母

print('\nsadad'.lstrip())#從左邊去掉空格回車

print('sadad\n'.rstrip())#從右邊去掉空格回車r

print('\nsadad\n'.strip())#全部去掉空格回車r

p = str.maketrans('fgijopuln','123456789')

print('liufong'.translate(p))#英文轉譯成數字

print('liu fong'.replace('l','l'))#前字母大寫

print('liu fong'.rfind('o'))#從左邊往右的下標,最後的值

print('liu fong'.split('l'))#以l作為切割符,轉成列表

print('1+2+3+4'.split('+'))#

print('1+2\n+3+4'.splitlines())#

print('liu fong'.swapcase())#英文全部大寫

print('liu fong'.title())#首字母大寫

print('liu fong'.zfill(50))#50個字串不夠用0填寫

python 字串基本操作

字串的基本操作 import operator s hello,world 去掉空格 換行符 s.strip 從左側切掉 s.lstrip hello,從右測切掉 a s.rstrip world 字條串拼接 s2 to me a s s2 查詢第乙個出現該字元的位置 a s.index o a s...

python 字串基本操作

一 引號 單引號 雙引號 三引號內都是字串,三引號的支援換行 字串內本身需要輸出引號,需要用反斜槓進行轉義,也可以直接在前面加個 r 例如 print r asd asd asd qwe 輸出 asd asd asd qwe 二.下標 索引 從0開始,用 0 框住 name yexueqing pr...

Python字串的基本操作

str字串有哪些操作 mystr.find str,start,end 如果存在,返回下標,不存在返回 1 mystr.index str,start,end 如果存在,返回下標,不存在報異常 mystr.count str,start,end 返回str在start到end之間出現的次數 myst...