In learning Python, we must read this knowledge again. It doesn't matter if we can't remember it. We must know the common function usage of strings

str.index(sub, start=None, end=None) Function...

str.index(sub, start=None, end=None)

Function: check whether the sub is in the string. If yes, the index will be returned, and only the index matched for the first time will be returned; If it cannot be found, an error will be reported; You can specify the statistical range, [start,end) left closed interval and right open interval

str = "helloworldhhh" print(str.index("h")) print(str.index("hhh")) # print(str.index("test")) directly reports syntax error: ValueError: substring not found

results of enforcement

0 10

str.find(sub, start=None, end=None)

Function: the same as index(), but no error will be reported if it is not found, but - 1 will be returned

str = "helloworldhhh" print(str.find("h")) print(str.find("hhh")) print(str.find("test"))

results of enforcement

0 10 -1

str.count( sub, start=None, end=None)

Function: count the number of substrings; you can specify the statistical range, [start,end) left closed interval and right open interval

''' No one answers the problems encountered in learning? Xiaobian created one Python exchange of learning QQ Group: 725638078 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book! ''' str = "hello world !!! hhh" print(str.count(" ")) print(str.count(" ", 5, 10))

results of enforcement

3 1

str.split(str="", num=string.count(str))

Function: divide strings into lists according to str. if the parameter num has a specified value, separate num+1 substrings

str = "hello world !!! hhh" print(str.split(" ")) print(str.split(" ", 1))

results of enforcement

['hello', 'world', '!!!', 'hhh'] ['hello', 'world !!! hhh']

str.strip(chars = " ")

Function: removes the character sequence chars specified at the beginning and end of the string. The default is space

str.lstrip(chars = " ")

Function: removes the character sequence chars specified in the string header. The default is space

str.rstrip(chars = " ")

Function: removes the character sequence chars specified at the end of the string. The default is space

str = " hello every " print("1", str.strip(), "1") print(str.lstrip(), "1") print("1", str.rstrip()) str = "!!! cool !!!" print(str.strip("!"))

results of enforcement

1 hello every 1 hello every 1 1 hello every cool

str.replace(old,new,count= -1)

Function: replace old (old string) with new (new string) in the string. count represents the maximum number of replacement times. The default - 1 represents all replacement

''' No one answers the problems encountered in learning? Xiaobian created one Python exchange of learning QQ Group: 725638078 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book! ''' str = "hello world !!! hhh" print(str.replace(" ", "-")) print(str.replace(" ", "-", 1))

results of enforcement

hello-world-!!!-hhh hello-world !!! hhh

str.join(sequence)

Function: connect the elements in the sequence with the specified characters to generate a new string

lists = ["1", "2", "3"] tuples = ("1", "2", "3") print("".join(lists)) print("".join(tuples)) print("-".join(lists))

results of enforcement

123123 1-2-3

str.upper()

Function: change all strings into uppercase letters

str.lower()

Function: change all strings into lowercase letters

str = "hello world !!! hhh"

print(str.upper()) print(str.lower())

results of enforcement

HELLO WORLD !!! HHH hello world !!! hhh

str.startswith(prefix, start=None, end=None)

Function: check whether the string starts with the specified substring. If so, return True; otherwise, return False; you can specify the statistical range, [start,end) left closed interval and right open interval

str.endswith(self, suffix, start=None, end=None)

Function: on the contrary, this is the end

''' No one answers the problems encountered in learning? Xiaobian created one Python exchange of learning QQ Group: 725638078 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book! ''' str = "hello world !!! hhh" print(str.startswith("h")) print(str.startswith("hh")) print(str.endswith("h")) print(str.endswith("hhhh"))

results of enforcement

True False True False

str.isdigit()

Function: check whether the string is only composed of numbers

str = "123134123" print(str.isdigit())

results of enforcement

true

str.isalpha()

Function: check whether the string is only composed of letters

str = "abc"

print(str.isalpha())

results of enforcement

true

str.splitlines([keepends])

Function: separate strings by lines ('\ r', '\ r\n', \ n ')

str = """ 123 456 789 """ print(str.splitlines()) with open("./file1.txt", encoding="utf-8") as f: lists = f.read().splitlines() print(lists)

results of enforcement

['', '123', '456', '789'] ['name: Jack ; salary: 12000', ' name :Mike ; salary: 12300', 'name: Luk ; salary: 10030', ' name :Tim ; salary: 9000', 'name: John ; salary: 12000', 'name: Lisa ; salary: 11000']

At the end, I recommend a very good learning tutorial for you. I hope it will be helpful for you to learn Python!

Python basics tutorial recommendation

Python crawler case tutorial recommendation

6 November 2021, 02:21 | Views: 7943

Add new comment

For adding a comment, please log in
or create account

0 comments