split() / join() 拆分和組合
#split() 通過指定分隔符對字符串進行切片(拆分),默認空格符 lan = "python ruby c c++ swift" lan.split() #['python', 'ruby', 'c', 'c++', 'swift'] #傳入符號',' todos = "download python, install, download ide, learn" todos.split(', ') #['download python', 'install', 'download ide', 'learn'] #把它們組合成字符串 ','.join(['download python', 'install', 'download ide', 'learn']) #'download python,install,download ide,learn'
replace() 替換
#replace() 第三個參數為代替幾個 s = 'I like C. I like C++. I like Python' s.replace('like', 'hate') #'I hate C. I hate C++. I hate Python' s.replace('like', 'hate', 1) #'I hate C. I like C++. I like Python'
布局center() : 前后留空 ljust():后面留空 rjust():前面留空
align = 'Learn how to align' align.center(30) #' Learn how to align ' align.ljust(30) #'Learn how to align ' align.rjust(30) #' Learn how to align' ralign = align.rjust(30) ralign.strip() #'Learn how to align'
eval(): 將字符串處理為可以運算的類型
# eval() str1 = '1+2' print(str1) #輸出1+2 print(eval(str1)) #輸出3
format() :格式化字符串 (New style in Python 3.6)
format(輸出項,[,格式化字符串])
格式化輸出 1)利用字符串格式化運算符%。 格式化字符串%(輸出項1,輸出項2....)
%% 百分號
%s 字符串
%c 字符
%d 帶符號整數 (10進制)
%o 帶符號整數 (8進制)
%x或者%X 帶符號整數(16進制)
%e或者%E 浮點數字(科學計數法)
%f或者%F 浮點數字(帶小數點)
%g或者%G 根據大小來選擇
%e %f %*.*f %(項1,項2,輸出項) 例如 %*.*f%(6,2,3.145) 等同 %6.2f%3.145
#格式化字符串的函數 str.format(),它增強了字符串格式化的功能。基本語法是通過 {} 和 : 來代替以前的 % print('%s %s' % ('one', 'two')) print('{} {}'.format('one', 'two')) print('%d %d' % (1, 2)) print('{} {}'.format(1, 2)) #one two #one two #1 2 #1 2 print('{1} {0}'.format('one', 'two')) #two one a = 5 b = 10 print(f'Five plus ten is {a + b} and not {2 * (a + b)}.') #Five plus ten is 15 and not 30. ''' 格式字符串還可以指定填充字符、對齊方式(其中<表示左對齊,>表示右對齊、^表示居中、==代表填充字符位於 符號和數字之間)、符號(其中+表示正號,-表示負號) ''' >>>format(65,"c") A >>>print(format(3.145,"6.2f")) 3.15 >>>print(format(3.145,"-6.2f")) 3.15 >>>print(format(3.145,"+6.2f")) +3.15 >>>print(format(3.145,"<6.2f")) 3.15 >>>print(format(3.145,">6.2f")) 3.15 >>>print(format(3.145,"><6.2f")) 3.15>> >>>print(format(3.145,"<=6.2f")) <<3.15 >>>print(format(3.145,"=6.2f")) 3.15 >>>print(format(3.145,"^6.2f")) 3.15 >><print(format(3.145,"0=+10")) +00003.145
其他
#以下函數都是返回新的值,不影響原來的字符串 kk = "Python description: Python is a programming language that lets you work quickly and integrate systems more effectively." kk.startswith('Python') #True kk.endswith('effectively.') #True kk.find('language') #44 #檢測字符串是否由字母和數字組成 kk.isalnum() #False kk.count("Python") #2 #移除字符串頭尾指定的字符(默認為空格或換行符)或字符序列 kk.strip('.') #'Python description: Python is a programming language that lets you work quickly and integrate systems more effectively' kk.upper() #'PYTHON DESCRIPTION: PYTHON IS A PROGRAMMING LANGUAGE THAT LETS YOU WORK QUICKLY AND INTEGRATE SYSTEMS MORE EFFECTIVELY.' kk.lower() #'python description: python is a programming language that lets you work quickly and integrate systems more effectively.' kk.title() #每個單詞開頭都大寫 #'Python Description: Python Is A Programming Language That Lets You Work Quickly And Integrate Systems More Effectively.' kk.capitalize() #首個單詞開頭大寫,后面不大寫 #Python description: python is a programming language that lets you work quickly and integrate systems more effectively. kk.swapcase() #每個字母大小寫調換 #pYTHON DESCRIPTION: pYTHON IS A PROGRAMMING LANGUAGE THAT LETS YOU WORK QUICKLY AND INTEGRATE SYSTEMS MORE EFFECTIVELY.