In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to express the string of Python". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to express the string of Python".
The representation of a string
Single quotation mark "
Double quotation marks "
Multiple quotation marks ","
Print ("hello world") print ("hello world") print ("hello world") # output result hello worldhello worldhello world
Why do you need single quotation marks and double quotation marks
Because you can include double quotes in single quotation marks or single quotation marks in double quotation marks
# single and double quotation marks print ("hello" poloyy "world") print ("this is my name" poloyy "") # output result hello "poloyy" worldthis is my name "poloyy" multiline string
Normally, single-quote and double-quote strings do not support line wrapping between symbols, and multiple quotation marks can be used if necessary.
# Multiline string print ("helloworld") print ("thisismynamepoloyy") # output result helloworldthisismynamepoloyy escape character
Just add it before the character.
The common ones are
: line break
Indent
Enter
Chestnut
For example, if there is a double quotation mark between the double quotes of a string, you need to use escape characters
# escape character print ("hello" poloyy "world") print ("my name is" poloyy "") # output result hello "poloyy" worldmy name is "poloyy"
Suppose you only want to be treated as ordinary characters?
Print ("what is a backslash") print ("what is a newline character") # output what is a backslash? what is a newline character?
Chestnuts in the window path
Print ("c:othingtype") print ("c:othingtype") # output result c:othingc:typec:othingtype
A more concise solution
Using escape characters can lead to poor readability and maintainability. Python provides a better solution: add r before the string.
Print (r "c:othingtype") # output result c:othingtype string operation: subscript and slice
Get a character in a string
A string is a sequence, so you can get a character by subscript
# get a character string str = "hello world" print (str [0]) print (str [1]) print (str [6]) print (str [- 1]) print (str [- 5]) # output result hewdl
If it is negative, it is the reciprocal. For example,-1 is the first element from the bottom, and-5 is the fifth element from the bottom.
Get a segment of a character in a string
In Python, you can take a character directly by slicing.
Grammatical format of slices
Str [start: end: step]
Start: closed interval, containing the character of the subscript, the first character is 0
End: an open range that does not contain the characters of the subscript
Step: step size
Chestnut
Print ("hello world" [:] "," hello world "[:]) # take all characters print (" hello world "[0:]", "hello world" [0:]) # take all characters print ("hello world" [6:] "," hello world "[6:]) # take the seventh character to the last character print (" hello world "[- 5:]" "hello world" [- 5:]) # take the penultimate character to the last character print ("hello world" [0:5] "," hello world "[0:5]) # take the 1st character to the 5th character print (" hello world "[0print 5]", "hello world" [0hello world 5]) # take the 1st character until the penultimate sixth character print ("hello world" [6:10] " "hello world" [6:10]) # take the 7th character to the 10th character print ("hello world" [6hello world Murray 1] "," hello world "[6hello world Murray 1]) # take the 7th character to the penultimate character print (" hello world "[- 5print 1]" "hello world" [- 5hello world]) # take the penultimate character to the penultimate character print ("hello world" [::-1] "," hello world "[::-1]) # reverse all characters print (" hello world "[:: 2]", "hello world" [:: 2]) # step = 2 Take print ("hello world" [1:7:2] "," hello world "[1:7:2]) # step = 2 every two characters, taking the 2nd character to the 7th character Take every two characters # output result hello world "[:] hello worldhello world" [0:] hello worldhello world "[6:] worldhello world" [- 5:] worldhello world "[0:5] hellohello world" [0worldhello world 5] hellohello world "[6:10] worlhello world" [6hello worldhello world 1] worlhello world "[- 5hello worldhello world 1] worlhello world" [::-1] dlrow ollehhello world "[:: 2] hlowrdhello world" [1:7:2] el thank you for your reading The above is the content of "how to represent the string of Python". After the study of this article, I believe you have a deeper understanding of how to express the string of Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.