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/02 Report--
This article mainly shows you "how to understand the\ characters in the string in Python development", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to understand the\ characters in the string in Python development".
The\ character in a string in Python development has three different meanings:
1. Escape character
2. The path name is used to connect the path name.
3. Write too long code to manually soft wrap.
A backslash is an "escape character"-changing the meaning of the following characters, such as:
\ r: enter,\ n: line feed,\ t: tab..
In order to express the character "backslash (\)", you need to precede it with a backslash:\
That is to say, the\\ in the string you see is a "\".
Another: you can use os.path.join ("C:", * dirs) to complete this path string concatenation.
1, when the backslash does not contain escape characters, it can still be printed, the result remains the same, but is not encouraged
> mystr='C:\ Program Files\ python\ Good'
> mystr
'C:\\ Program Files\\ python\\ Good'
> print (mystr)
C:\ Program Files\ python\ Good
2, when the backslash and the characters that follow it are combined into escape characters, the output will change
> mystr='C:\ numpyPacket\ Program Files\ python\ Good'
> mystr
'C:\ numpyPacket\ Program Files\\ python\\ Good'
> print (mystr)
C:
UmpyPacket\ Program Files\ python\ Good
3. The solution to the above situation is to output the position of all backslashes, using the escape character\, and then output itself. The second is to use the original string r, that is, to add a small r before the string.
Mystr='C:\\ numpyPacket\\ Program Files\ python\\ Good'
> mystr
'C:\\ numpyPacket\\ Program Files\\ python\\ Good'
> print (mystr)
C:\ numpyPacket\ Program Files\ python\ Good
> mystr=r'C:\ numpyPacket\ Program Files\ python\ Good'
> mystr
'C:\\ numpyPacket\\ Program Files\\ python\\ Good'
> print (mystr)
C:\ numpyPacket\ Program Files\ python\ Good
4. If you want to concatenate a backslash or a double backslash at the end of a string, first of all, it is not feasible to add a backslash directly to the end of the string or to add a double backslash using escape characters mystr='C:\ numpyPacket\ Program Files\ python\ Good\'
SyntaxError: EOL while scanning string literal
Mystr='C:\\ numpyPacket\\ Program Files\ python\\ Good\\'
SyntaxError: EOL while scanning string literal
> mystr=r'C:\ numpyPacket\ Program Files\ python\ Good\'
SyntaxError: EOL while scanning string literal
>
The correct action:
There is only a slash at the end of the string:
> > mystr='C:\\ numpyPacket\\ Program Files\\ python\\ Good\\'
> mystr
'C:\\ numpyPacket\\ Program Files\\ python\\ Good\\'
> print (mystr)
C:\ numpyPacket\ Program Files\ python\ Good\
There are 2 slashes at the end of the string:
> mystr='C:\\ numpyPacket\\ Program Files\\ python\\ Good\'
> mystr
'C:\\ numpyPacket\\ Program Files\\ python\\ Good\
> print (mystr)
C:\ numpyPacket\ Program Files\ python\ Good\\
> mystr=r'C:\ numpyPacket\ Program Files\ python\ Good\'
> mystr
'C:\\ numpyPacket\\ Program Files\\ python\\ Good\
> print (mystr)
C:\ numpyPacket\ Program Files\ python\ Good\\
The above is all the contents of the article "how to understand\ characters in strings in Python development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.