python string on the basis of common operations

1. If you want to string the first letter capitalized, you can call captailze function.

str="fasfaefeaw"

d=str.captailze()

print(d)

2. If you want case-string reverse function can be called swapcase

str="fhaewhklf"

d=str.swapcase()

print(d)

3. If you want to capitalize the first letter of each item, you can call the function title

str="fea faef  efaf"

d=str.title()

print(d)

4 If you want to center your text, you can call center functions

str="feawfeawf"

d = str.center (20) # 20 refers to the width of the string

print(d)

5. The method of expandtabs function is on, if the string has / t, then / t behind the first letter is automatically filled and eight spaces in front of

str="feawfe/tafeaw"

d=str.expandtabs()

print(d)

6. Analyzing the string begins with anything starwith function, if it is determined correctly, return Ture, whether those returns False. startwith function if the function is nothing, then, is the default string all began

str="feawfe"

d = str.starwith ( "fe") # d = str.startwith ( "e", 1,2) this means a start from index to index two stops, see the beginning of this section, and whether an input consistent.

print(d)

Use 7.find () function, the function input in find () you are looking for letters, will find you find the letters of the index, if not, then returns -1, you can find a slice

str="faeafeft"

d = str.find ( "t") # dstr.find ( "t", 2,7) indicates there are no t in the range of 2 to 7

print (d, type (d)) # where this type of representation is to determine the type of data you're looking for

8.index () function and the find () method using the function is the same, it can be used slice, not the same, if not, then the return is not the same, if not found returns -1 find () in , while the index () function, if not, then error.

9.strip () function to use, strip used to remove the spaces before and after the default function, if there are things that can also delete other things, this same also rstrip, rstrip mean start deleting from the right, the left does not move , lstrip meaning start deleting from the left, not the right move

str="faefeaw#"

d = str.strip () # d = str.strip (#) where # is the number of the deleted

print(d)

username = input ( "Please enter a name:"). strip ()

if username == "Wang":

  print ( "correct answer")

Usage 10.count () function, a number of elements which appear for calculating sections can also be used

str="faefeaf"

d=str.count("f")#d=str.count("f",0,8)

print(d)

11. The segment element, generally using split () function, split inside pass what is splitting, splitting pass after things that do not exist, transformed into a space. split () string to show up in a list

str="feaw; feawf; afeawf;feawf"

d = str.spilt (;) # Here is a list segmentation, semicolon will disappear, transformed into a space

12.format () formatted output

s = "My name is {}, {older}, {} hobbies, again I'm {}". format ( "Wang", 20, "billiards", "Wang")

s = "My name is {0}, {1} the age, hobbies {2}, again I'm {0}." the format ( "Wang", 20, "billiards")

s = "My name is {name}, older {age}, hobby {habby}, repeat my name {name}". format (name = "Wang", age = 20, habby = "billiards")

print(s)

13.replace () function usage

str = "I like playing basketball I like."

d = str.replace ( "I like it", "Wang likes", 1)

Guess you like

Origin www.cnblogs.com/648071634com/p/11640902.html