Home > Backend Development > Python Tutorial > Let's take a look at Python's commonly used strings and their operations

Let's take a look at Python's commonly used strings and their operations

coldplay.xixi
Release: 2021-03-16 10:07:18
forward
2935 people have browsed it

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"))
Copy after login

Lets take a look at Pythons commonly used strings and their operations

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    "))
Copy after login

Lets take a look at Pythons commonly used strings and their operations

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())
Copy after login

Lets take a look at Pythons commonly used strings and their operations

lower(str) conversion Uppercase letters are lowercase letters (equivalent to regenerating a string)

4.upper(str)

str2 = "man"print(str2.upper())
Copy after login

Lets take a look at Pythons commonly used strings and their operations

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())
Copy after login

Lets take a look at Pythons commonly used strings and their operations
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, "*"))
Copy after login

Lets take a look at Pythons commonly used strings and their operations

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, "*"))
Copy after login

Lets take a look at Pythons commonly used strings and their operations

##center(width , filch) returns a centered specified string, with default space filling

9.zfill(width)

str6 = "man!"print(str6.zfill(40))
Copy after login

Lets take a look at Pythons commonly used strings and their operations

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)))
Copy after login

Lets take a look at Pythons commonly used strings and their operations

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"))
Copy after login

Lets take a look at Pythons commonly used strings and their operations##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())
Copy after login

Lets take a look at Pythons commonly used strings and their operationstitle()Capitalize the first letter of each word

13.capitalize()

str7 = "tHAtwelikEcLOSEclosefRIEnds"print(str7.capitalize())
Copy after login

Lets take a look at Pythons commonly used strings and their operationscapitalize()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"))
Copy after login

Lets take a look at Pythons commonly used strings and their operationsindex(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("*"))
Copy after login

Lets take a look at Pythons commonly used strings and their operationslstrip() 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 visit

python 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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template