Home >Backend Development >Python Tutorial >Let's take a look at Python's commonly used strings and their operations
Commonly used strings in python
We will have some commonly used strings when learning python. I will There is some sorting out, there are actually many more, here are just examples.
1.eval(str)
print("12+3")print(eval("12+3"))
The strings we usually input in print are directly output , and eval(str) can treat the string str as a valid expression and return the calculation result
(free learning recommendation: python video tutorial )
2.len(str)
print(len("man"))print(len("man "))
len(str) return character The length of the string (number of characters) and spaces are also characters
3.lower(str)
str1 = "MAN"print(str1.lower())
lower(str) conversion Uppercase letters are lowercase letters (equivalent to regenerating a string)
4.upper(str)
str2 = "man"print(str2.upper())
upper( str) converts lowercase letters to uppercase letters (equivalent to regenerating a string) and is just the opposite of lower()
5.swapcase()
str3 ="so that WE LIKe close FriENds"print(str3.swapcase())
swapcase() converts the lowercase letters in the string to uppercase and the uppercase letters to lowercase
6.ljust(width[,filch])
str4 = "man"print(str4.ljust(40, "*"))
ljust(width[, filch]) returns a left-aligned string of specified width, filch is the fill character, and the default is space
7.rjust(width[, filch ]) In the same way, it is right aligned
8.center(width, filch)
str5 = "man!"print(str5.center(40, "*"))##center(width , filch) returns a centered specified string, with default space filling
9.zfill(width)
str6 = "man!"print(str6.zfill(40))zfill( width) returns a string with a length of width. The original string is right-aligned and prepended with 0
10.count(str[,start][,end])
str7 = "so that we like close close friends"print(str7.count("close"))print(str7.count("close", 22, len(str7)))count(str[,start][,end]) returns the number of occurrences of str in the string. You can specify a range. The default is from beginning to end
11.find(str[,strat][,end])
str7 = "so that we like close close friends"print(str7.find("close"))print(str7.find("man"))
##find(str[,strat][,end])from left to right Check whether the str string contains the searched string. You can specify the range. The default is from beginning to end. What you get is the starting subscript that appears for the first time. No -1
12.title() is returned.str7 = "so that we like close close friends"print(str7.title())
title()Capitalize the first letter of each word
13.capitalize()str7 = "tHAtwelikEcLOSEclosefRIEnds"print(str7.capitalize())
capitalize()The first letter is capitalized, and the other letters are lowercase
14.index(str, start=0, end=len(str)str7 = "so that we like close close friends"print(str7.index("we"))print(str7.index("is"))
index(str, start=0, end=len(str)) is the same as find, but if str does not exist, an exception will be reported
15.lstrip()
str8 = " a good man!"str9 = "***** a good man!"print(str8.lstrip())print(str9.lstrip("*"))
lstrip() intercepts the specified characters on the left side of the string. The default is a space. As you can see, write After intercepting the parameters, the spaces will not be intercepted
I am still learning, please correct me if there are any mistakes
There are a lot of free learning recommendations, please visitpython tutorial(Video)
The above is the detailed content of Let's take a look at Python's commonly used strings and their operations. For more information, please follow other related articles on the PHP Chinese website!