Python3 string manipulation

Source: Internet
Author: User

字符串操作:1、 下载python官方手册2、 先定义一个字符串变量 A = ‘abc’ A.两次TAB键 help(A.选项) #查看帮助

>> ' ABC '. Lower () #XXX. Lower converts uppercase strings to lowercase
' ABC '

XXX.title()     #将字符串每个单词的首字母转换为大写XXX.capitalize()    #将字符串的首字母转换为大写XXX.center(长度, 充填符)   #字符串居中XXX.ljust(宽度, 充填符号)  #字符串向左对齐XXX.rjust(宽度, 充填符号)  #字符串向右对齐XXX.count(‘字符‘)            #统计单个字符出现的次数

>> hi = ' Hello world '
>> hi.startswith (' l ') #判断行首是否为L, correct but true, error returned false
False
>> hi.startswith (' h ')
True
>> hi.endswith (' d ') #判断结尾
True

>> a = ' [email protected]#$ '
>> A.islower () # # #XXX. Islower returns True if all lowercase in the string is determined, otherwise false is returned. Ignore numbers, characters
True
>> a = ' [email protected] '
>> A.islower ()
False

>> a = ' [email protected]#%! '
>> A.isupper () #判断字符串中是否全部为大写, yes returns true, otherwise false is returned. Ignore numbers, write characters, or return true, otherwise false. Ignore numbers, characters
True

>> a = ' WGWEGWEGWEWASDG '
>> A.isalpha () #判断是否全部为字母, ignoring case, character, number
True
>> a = ' WGWEGWEGWEWASDG1 '
>> A.isalpha ()
False

>> ' 434 '. IsDigit () #判断字符串是否全部是数字
True
>> ' 434a '. IsDigit ()
False

>> ' ABC123 '. Isalnum () #判断是否存在符号
True
>> ' \t\r\n '. Isspace () #是否为空白字符
True

####### #去除字符串两端空白

>> a = ' \thello world '
>> A
' \thello World '
>> A.strip ()
' Hello World '
>> A.lstrip () #去除左边
>> A.rstrip () #去除右边

# # # #XXX. Strip () remove the specified characters at both ends

>> a = ' Hello world! '
>> a.strip ('! ')
' Hello World '
>> a.strip ('!he ')
' Llo World '
>> a.strip ('!dhe ')
' Llo worl '
>> A
' Hello world! '
>> A.strip (' W ') #无法去除中间字符
' Hello world! '

>> hi = ' Hello,world '
>> hi.split () #把字符串进行转换, convert to list,
[' Hello,world ']

>> hi.split (', ') #指定分割字符串的分隔符
[' Hello ', ' world ']

>> hi.replace (' O ', ' a ') #替换字符
' Hella,warld '

Python3 string manipulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.