Python Learning Notes (iv) string and string manipulation

Source: Internet
Author: User
Tags uppercase letter

String

Strings can be stored in any type of string, such as a name, a sentence, and so on.

Strings also have many built-in methods for manipulating strings, often in the following ways:

1name1='Hello World'2 Print(Name.capitalize ())#Capitalize first letter3 Print(Name1.capitalize ())#Write letter uppercase Hello world4 Print(Name1.center (50,'-'))#50-, put name1 in the middle-------------------Hello World--------------------5 Print(Name.endswith ('u'))#whether to end with X False6 Print(Name1.endswith ('D'))#whether to end with X True7 Print(Name.expandtabs (30))#the number of times that the \ t was filled in my name is {name},age is{age}.8 Print(Name1.find ('N'))#find the index of the string 5 (\ t represents 3 characters)9 Print(Name.format (name='Niuniu', age=18))#This is the format string my name is Niuniu,age is18.Ten Print(Name.format_map ({'name':'Niuniu',' Age': 19}))#This is also the format of the string, followed by a dictionary, the dictionary will be written in the back One #My name is Niuniu,age is19. A Print('abA123'. Isalnum ())#Whether it contains numbers and letters True - Print('AbA'. Isalpha ())#is there an English letter true
1Name ='My \ t name is {name},age is{age}.'2 Print('122'. IsDigit ())#whether there is a number true3 Print('AA'. Isidentifier ())#is a valid variable name true4 Print('AA'. Islower ())#whether it is a lowercase letter true5 Print('AA'. Isupper ())#is the uppercase letter true6 Print('LoadRunner Book'. Istitle ())#is not a title that determines whether the first letter is capitalized true7 Print('+'. Join (['hehe','haha','ee']))#Stitching String Hehe+haha+ee8 Print(Name.lower ())#becomes lowercase my name is {name},age is{age}.9 Print(Name.upper ())#becomes uppercase my NAME is {name},age is{age}.Ten Print('\nmysql \ n'. Lstrip ())#by default, the left space and line breaks are removed One Print('\nmysql \ n'. Rstrip ())#by default, the right space and line breaks are removed A Print('\nmysql \ n'. Strip ())#by default, both spaces and line breaks are removed -P=str.maketrans ('ABCDEFG','1234567')#the preceding string and the following string do the mapping - Print('CC AE GG'. Translate (p))#the output is mapped in accordance with the above Maketrans string .
1New_p=str.maketrans ('1234567','ABCDEFG')2 Print('CC AE GG'. Translate (new_p))3 Print('MySQL is db.'. replace ('MySQL','Oracle', 1))#Replace the string Oracle is DB.4 Print('MySQL is db.mysql are Db.mysql is db.'. replace ('MySQL','Oracle', 2))#Replacement String Oracle is Db.oracle are Db.mysql is db.5 Print('MySQL is an IS db'. RFind (' is'))#returns the subscript of the rightmost character 96 Print('1+2+3+4'. Split ('+'))#cut string, return a list [' 1 ', ' 2 ', ' 3 ', ' 4 ']7 Print('1+2+3\n1+2+3+4'. Splitlines ())#split by line break [' 1+2+3 ', ' 1+2+3+4 ']8 Print('Abcdef'. Swapcase ())#Case Reversal ABCdef

Python Learning Notes (iv) string and string manipulation

Related Article

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.