Python Learning Note (ii)-string method

Source: Internet
Author: User

Python string built-in functions:

#==================== Common Methods =============================
Name ='besttest'new_name= Name.strip ()#The default is to remove both spaces and line breaksNew_name= Name.lstrip ()#The default is to remove the left space and line breakNew_name= Name.rstrip ()#The default is to remove the space and line breaks on the right sideNew_name = Name.count ('T')#find the number of occurrences of a string within a stringindex = Name.find ('D')#find the string, return its subscript, or 1 if it does not exist .index = Name.index ('D')#find this string, return its subscript, if it does not exist, errorPrint(Name.upper ())#to capitalize all the letters.Print(Name.lower ())#to turn all the letters into lowercase.file_name ='A.xls'Print(File_name.endswith ('. xls'))#determines whether a string ends in XXsql ='select * from user; Select'Sql.startswith ('Select')#determines whether a string begins with XXf ='{Name} Welcome to age: {Age}'Print(F.format (name='Caocao'))#string FormattingD = {'name':'Yuanbaoqiang',' Age': 18}Print(F.format_map (d))#string formatting, which is passed in a dictionaryNew_sql = Sql.replace ('Select','Update')#string substitution, first old,newPrint(New_sql)Print('122s'. IsDigit ())#whether it is a numberPrint('&'. Isalnum ())#Whether it contains numbers or lettersSt ='a b c D f g'st_list= St.split ()#If you don't write anything, divide it by the space.Print(st_list)#=========== Less common method ====================new_name= Name.capitalize ()#Capitalize first letterName.center (50,'*')#Put the string in the middle, both sides with *Print('AaAA'. Islower ())#whether it is a lowercase letterPrint('AAaa'. Isupper ())#whether it is a capital letterPrint('SDFSDF'. Isalpha ())#whether it is an English letter

Python Learning Note (ii)-string method

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.