In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the common methods of python strings". In daily operation, I believe many people have doubts about the common methods of python strings. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the common methods of python strings?" Next, please follow the editor to study!
What is a string?
A string is a series of characters. Strings belong to immutable sequences. In python, quotation marks are all strings wrapped in quotation marks, in which quotation marks can be single, double or triple quotation marks (characters in single and double quotation marks must be on one line, and characters in three quotation marks can be distributed on multiple lines)
Txt = 'hello world' # use single quotation marks and the string content must be on one line
Txt1 = "hello python world" # use double quotation marks and the contents of the string must be on one line
# using three quotation marks, the contents of the string can be distributed over multiple lines
Txt2 = 'life is short
I use python''
The common method of string 1.find ()
Define the find () method to return the minimum index value of the element (- 1 cannot be found). Give me a chestnut. Returns the minimum index value of "python"
Txt = "hello python world." res = txt.find ("python") print (res)
The running results are as follows:
six
2.index ()
Define the index () method to return the minimum index value of the element (an error will be reported if the element is not found). Give me a chestnut. Returns the minimum index value of "world"
Txt = "hello python world." res = txt.index ("world") print (res)
The running results are as follows:
thirteen
3.startswith ()
Defines the startswith () method that returns True if the string begins with a specified value, otherwise returns False. Give me a chestnut. Determine whether the string begins with "hello"
Txt = "hello python world." res = txt.startswith ("hello") print (res)
The running results are as follows:
True
4.endswith ()
Defines the endswith () method that returns True if the string ends with a specified value, otherwise False. Give me a chestnut. Determine whether the string ends with "hello"
Txt = "hello python world." res = txt.endswith ("hello") print (res)
The running results are as follows:
Flase
5.count ()
The definition count () method returns the number of times the specified value appears in the string. ? Give me a chestnut. Count the number of "o" occurrences
Txt = "hello python world." res = txt.count ("o") print (res)
The running results are as follows:
three
6.join ()
Define the join () method to get all the items in the iterable object and concatenate them into a string. The string must be specified as the delimiter. Give me a chestnut. Use "-" as a delimiter to concatenate all items in the list into a string
Res = ['hindsight print ('-'.join (res))
The running results are as follows:
H-e-l-l-o
7.upper ()
Define the upper () method to convert all strings to uppercase. Give me a chestnut. Convert the string "hello python world" to uppercase
Tet = "hello python world" res = txt.upper () print (res)
The running results are as follows:
HELLO WORLD
8.lower ()
Define the lower () method to convert all strings to lowercase. Give me a chestnut. Convert the string "HELLO PYTHON WORLD" to lowercase
Tet = "HELLO PYTHON WORLD" res = txt.lower () print (res)
The running results are as follows:
Hello python world
9.split ()
Define the split () method to split the string with the specified characters and return the list. Give me a chestnut. To? The number is used as a delimiter to split the string
Txt = "hello?python?world" res = txt.split (?) print (res)
The running results are as follows:
['hello',' python', 'world']
? Expansion? The split print is still the original string (the string is immutable, the split operation is to copy the original string, and the copy is changed)
Txt = "hello?python?world" res = txt.split ("?") # print split print (res) # print the original string print (txt) ['hello',' python', 'world'] hello?python?world10.strip ()
Define the strip () method to delete spaces at both ends of a string. Give me a chestnut. Delete spaces at both ends of the hello
Txt = "hello" res = txt.strip () print (res)
The running results are as follows:
Hello
11.replace ()
Define the replace () method to replace the specified content with the specified content (replace all by default, but specify the number of replacements). Give me a chestnut. Replace python with java
Txt = 'hello python world'res = txt.replace (' python','java') print (res)
The running results are as follows:
Hello java world
? Expansion? Print or the original string after replacement (the string is immutable, the replacement operation is to copy the original string, and the copy is changed)
Txt = 'hello python world'res = txt.replace (' python','java') # print replaced print (res) # print original string print (txt)
Hello java world
Hello python world
At this point, the study of "what are the common methods of python strings" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.