Python Basics (quad) string processing

Source: Internet
Author: User

String processing
msg = ‘my name is sylar‘
Capitalize method to capitalize the first letter of a string
print ‘capitalize方法:‘, msg.capitalize()
The Swapcase method is used to convert the uppercase and lowercase letters of a string. Upper
print ‘swapcase方法:‘, msg.swapcase()print ‘upper方法:‘,msg.upper()msq_lower = msg.upper()print ‘lower方法:‘,msq_lower, msq_lower.lower()
All words start with uppercase and the remaining letters are lowercase (see istitle ())
print ‘title方法:‘,msg.title()
Count method, number of occurrences of the query substring
print msg.count(‘name‘)print ‘count方法:‘, msg.count(‘a‘)
Center: Fills the string to the specified length with the specified character, left
print ‘center方法:‘, msg.center(40,‘x‘)
Ljust aligns the original string to the left and fills the new string with a space to the specified length. Returns the original character if the specified length is less than the length of the original string. The padding character, which defaults to a space.
print ‘ljust方法:‘, msg.ljust(20,‘x‘)
Rjust aligns the original string to the right, and fills the new string with a space of length width. Returns the original string if the specified length is less than the length of the string.
print ‘rjust方法:‘, msg.rjust(20,‘x‘)
Character encoding #decode, encode
msg2 = ‘我是sylar‘msg3 = u‘我是sylar‘print ‘msg2:‘,msg2,type(msg2),‘\tmsg3:‘, msg3, type(msg3)msg2 = msg2.decode(‘utf-8‘)msg3 = msg3.encode(‘utf8‘)print ‘msg2.decode:‘,msg2, type(msg2), ‘\tmsg3.decode:‘,msg3,type(msg3)
Endswith,startswith string to find the starting and ending positions of a string
print msg.endswith(‘sylar‘)print msg.endswith(‘tom‘)print msg.startswith(‘my‘)print msg.startswith(‘MY‘)
Expandtabs to replace tab tabs with spaces Windows is less obvious here. Linux below is the length of the tab change
msg_expandtabs = ‘my name is\tsylar‘print ‘expandtabs方法:‘,msg.expandtabs()
The Fing,index method #index检查是否包含一个子串位置码和find方法一样 but reports an error returning the!,find method does not return an error return-1
print ‘index方法:‘,msg.index(‘y‘)
print ' Index method returns error: ', Msg.index (' Sylra ')
print ‘find方法:‘,msg.find(‘y‘)print ‘find方法没找到:‘,msg.find(‘tom‘)
Rfind,rindex returns the last occurrence of a string where Find,index is the first occurrence
print ‘rfind方法:‘,msg.rfind(‘y‘)print ‘rindex方法:‘,msg.rindex(‘y‘)
Format formatted output string
msg.format()age = ‘88‘name = ‘sylar‘print(‘{0} is {1} years old. ‘.format(name, age)) #输出参数print(‘{0} is a girl. ‘.format(name))
String type judgment
age_num = age.isalnum() #所有字符都是数字或者字母age_dig = age.isdigit() #所有字符都是数字age_alp = age.isalpha() #所有字符都是字母age_low = age.islower() #所有字符都是小写age_spc = age.isspace() #所有字符都是空白字符age_tit = age.istitle() #所有单词都是首字母大写,像标题age_upp = age.isupper() #所有字符都是大写print ‘字符串类型判断:‘,age_num,age_dig,age_alp,age_low,age_spc,age_tit,age_upp
The Lstrip method is used to truncate the left space of a string or specify a character. Rstrip removes the specified character at the end of a string string, and the Strip method removes the character specified by the string (the default is a space)
msg_strip = ‘xxxkslfsjflsjxxx‘print ‘lstrip方法:‘, msg_strip.lstrip(‘x‘)print ‘rstrip方法:‘,msg_strip.rstrip(‘x‘)print ‘strip方法:‘,msg_strip.strip(‘x‘)
Intercepts a string of the specified length, aligns the original string to the right, and fills the front 0
print ‘zfill方法:‘,msg.zfill(10)
The Replace method replaces the string in the string, and if you specify the third argument count, the substitution does not exceed count times
print ‘replace方法‘,msg.replace(‘is‘,‘\033[31;1mis\033[0m‘)print ‘replace方法‘,msg.replace(‘is‘,‘\033[31;1m%s\033[0m‘%name)
Join generates a new string for the elements in the sequence with the specified character join
join_list = [‘1‘,‘2‘,‘3‘,‘4‘]msg_join =‘‘.join(join_list)print ‘join方法:‘, msg_join ,type(msg_join)
Split,rsplit format the string as a list, do not specify a cut word defaults think of a space, you can also specify the number of cuts, split from left to right, rsplit right to left (the default is a space)
msg_spt = msg.split(‘y‘,1)print msg_spt,type(msg_spt),msg_spt[0] #当列表内有中文是,直接打印列表返回Unicode编码,取值时则返回中文msg_spt = msg.rsplit(‘y‘,1)print msg_spt,type(msg_spt),msg_spt[0]
The partition method delimits a string as Ganso splits the string according to the specified delimiter.
print ‘partition方法:‘, msg.partition(‘y‘), type(msg.partition(‘y‘))print ‘rpartition方法:‘, msg.rpartition(‘y‘),type(msg.rpartition(‘y‘))
Splitlines Returns a list that contains rows as elements, separated by rows, and if num specifies that only num rows are sliced
msg_splitlines=‘1\n2\n3\n4‘print ‘splitlines‘,msg_splitlines.splitlines()
Translate () Converts the character of a string according to the table given by the parameter table (containing 256 characters), and the character to be filtered out is placed in the Del parameter.

Python Basics (quad) string processing

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.