Common string usage of python

1.isdigit=indecimal (judging whether it is a number)

s="123"
print(s.isdigit())

The output is: True

 

2.format(identifier)

a =("I have a {0},My name is {1}").format("dog","gao")
print(a)

The output is: I have a dog,My name is gao

 

3.join (splicing the list into a string, separated by --)

a =["bbb","aaa","ccc"]
print("--".join(a))

Output: ("bbb--aaa--ccc")

 

4.strip (remove the spaces, newlines, and Tabs on the left and right sides of the string)

s="    aaa      "
print(s.strip())

Output: (aaa)

 

5.replace (replace)

s="Hello World"
print(s.replace("o","a",2)) Replace o with a and replace 2 with o

Output: Hella Warld

 

6.find (find index)

s="Hello World"
print(s.find("o",0,5)) find the index of o in 0-5

output 4

 

7.split (split)

s="Hello World"
print(s.split("o",1)) split by an o

Output: ['Hell', 'World']

 

8.center

s="Hello World"
print(s.center(30,"-")) generates a title with a length of 30 and a - on both sides

Output:---------Hello World----------

 

9.count (number)

s="Hello World"
print(s.count("o",0,5)) Count o between 0-5

output: 1

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324997872&siteId=291194637