Python Learning Path A common method of-string strings

Source: Internet
Author: User

Uppercase and lowercase conversions

1 __author__="Kuankuan"2 Print("name is Jankinyu". Capitalize ())#Convert to first letter capitalization3 Print("name is Jankinyu". Title ())#To title Form Name is Jankinyu4 Print("NAMW". Lower ())#Convert to lowercase5 Print("name". Upper ())#Convert to uppercase6 Print("Name". Swapcase ())#uppercase to lowercase lowercase to uppercase7 """8 Output Result:9 Name is JankinyuTen Name is Jankinyu One NAMW A NAME - NAME - """

String judgments

1 __author__="Kuankuan"2 Print("name is Jankinyu". StartsWith ("name"))#to have a. True if correct start3 Print("name is Jankinyu". EndsWith ("Jankinyu"))#to have a. True if the end is correct4 Print("ASD". Isalpha ())#true if it is a letter5 Print("123". IsDigit ())#true if it is a number6 Print("asd123". Isalnum ())#true if it is a number or a letter7 Print("JJK". Isidentifier ())#determine if the legal variable name is valid for true8 Print(" ". Isspace ())#Space-->true9 Print("My Name is Jankinyu". Istitle ())#title-->trueTen Print("My Name is Jankinyu". Isprintable ())#printable TTY file, drive file-->true One Print("A". Isupper ())#Uppercase-->true A Print("a". Islower ())#lowercase-->true

Output display

1 __author__="Kuankuan"2 Print(",". Join (["1","2","3"]))#Connection3 #->1,2,34 Print("name is Jankinyu". Split ())#split into a list with a default splitter character as a space5 #->[' name ', ' is ', ' Alex '6 Print("name\n is\r\n Jankinyu". Splitlines ())#split into list, default splitter is carriage return7 #->[' name ', ' is ', ' Alex '8 Print("name". Center (50,"-"))#Center content, padding on both sides9 #-----------------------name-----------------------Ten Print("name". Ljust (50,"*"))#content on the left, filled with * One #->name********************************************** A Print("name". Rjust (50,"-"))#content on the right, to-fill - #----------------------------------------------name - Print("name". Zfill (50))#content on the right, filled with 0 the #->0000000000000000000000000000000000000000000000name - Print("name Is\t Jankinyu". Expandtabs (TABSIZE=18))#set ' \ t ' width - #->name is Jankinyu - Print("I am from {city},and {years}". Format (city="Kuankuan", year=18))#Formatting + #->i am from Wuhan,and to years old - Print("I am from {city},and {years}". Format_map ({" City":"Kuankuan"," Year": 18}))#Dictionary parameter Formatting + #->i am from Wuhan,and to years old A Print("\nname". Lstrip ())#Remove left carriage return and space->name at Print("name\n". Rstrip ())#Remove right carriage return and space->name - Print("name\n". Strip ())#Remove both carriage returns and spaces->name - Print("~~~~~~~~~~~~~~~~~~~~")

Find replacements

1 __author__="Kuankuan"2p = Str.maketrans ("ABC","123")#Create translation Dictionaries3 Print(Type (p))#--><class ' dict ' >4 Print("ABC". Translate (p))#translation, Parameters: Dict-->1235 Print("name is Jankinyu". replace ("N","N"))#Replace-->name is Jankinyu6 Print("name is Jankinyu". Count ("I"))#Character Count-->27 Print("name is Jankinyu". Find ("N"))#Find, return subscript, no then return-1-->28 Print("name is Jankinyu". RFind ("N"))#start looking for-->13 on the right9 Print("name is Jankinyu". Index ("N"))#returns the index, does not exist the error-->0Ten Print("name is Jankinyu". Rindex ("N"))#start looking for-->13 on the right One """ A Output Result: - <class ' dict ' > - 123 the Name is Jankinyu - 2 - 0 -  - + 0 -  - + """

Python Learning Path A common method of-string strings

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.