網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

string函數|python

欄目: IT科技 / 發佈於: / 人氣:1.62W
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

python string函數是什麼?一起來看下吧:

python string函數包括:

1、str.capitalize:將原字元串內的首字母轉成大寫,其他部分小寫,再返回新字元串

print("s.capitalize() = {function}"s.capitalize() = Abcada a

2、str.lower:將原字元串的字母轉爲小寫

print("s.lower() = {function}".format(function = s.lower()))s.lower() = abcada a

3、str.upper:將原字元串的字母轉爲大寫

print("s.upper() = {function}".format(function = s.upper()))s.upper() = ABCADA A

4、str.swapcase:將原字元串的大寫小寫反轉

print("s.swapcase() = {function}".format(function = s.swapcase()))s.swapcase() = ABCAdA A

5、str.title:原字元串內如果有特殊字元(包括數字)連接字母,則將特殊字元后的首個英文字母轉化爲大寫形態,並返回新字元串

print("s2.title() = {function}".format(function = s2.title()))s2.title() = 123A Abc Abcsaa S

python string函數

6、str.center:str.center(寬度,填充字元) 將字元串以居中的格式返回,若寬度值比len(s)小則返回原字元串,填充以從左到右爲規則,填充字元的默認值爲空格,值可以自己更改

print("s2.center() = {function}".format(function = s2.center(19,'&')))print("s2.center() = {function}".format(function = s2.center(20,'&')))#s2 = 123a abc ABCSAa ss2.center() = &123a abc ABCSAa s s2.center() = &123a abc ABCSAa s &

7、str.expandtabs:str.expandtabs(tabsize = 8) 將原字元串中以前的字元補滿8位(默認),tabsize的值從0-7即8位,在0-7中任意取值則默認tabsize = 8,此後往上+1,就相當於增加一個空格

print("s3.expandtabs ={function}".format(function = s3.expandtabs()))print("s3.expandtabs ={function}".format(function = s3.expandtabs(0)))print("s3.expandtabs ={function}".format(function = s3.expandtabs(5)))print("s3.expandtabs ={function}".format(function = s3.expandtabs(8)))print("s3.expandtabs ={function}".format(function = s3.expandtabs(9)))#s3 = "as b123"s3.expandtabs =        as              b123s3.expandtabs =as b123s3.expandtabs =     as        b123s3.expandtabs =        as              b123s3.expandtabs =         as

除了上述舉例的,string函數還有許多實用的函數。