当前位置:懂科普 >

IT科技

> python string函数

python string函数

<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函数还有许多实用的函数。

标签: 函数 string Python
  • 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/itkeji/7ov5z8.html