In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use Python strings. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
string
Strings in Python 3 can be marked with double quotes or single quotes, and if quotes appear in the string, you can use\to remove the effect of marking the string with quotes.
An index string is an ordered collection of characters whose position determines the element. In Python, characters in a string are extracted by indexing, starting at 0, with the first character having an index of 0, the second character having an index of 1, and so on.
Python string index can take negative values, indicating extraction from the end, the index of the last character is-1, the index of the penultimate character is
-2, i.e. the program thinks it can count backwards from the end.
A simple example:
A slice is a fragment of a character string obtained in the format [head index: tail index: step size]. Index is also called subscript.
Distance Description:
Common properties and methods of strings count
Gets the number of characters in a string:
split and strip
There are many common methods in strings that can be used. In Python Shell, you can use help(str) to view all the methods in strings. Here are two common methods.
strip(): By default, whitespace and newlines at the beginning and end of a string are removed. If parameters are used in the strip() function, the characters in these parameters are deleted (only if they appear at the beginning and end of the string), for example str1.strip ('ab ') deletes only the a and b characters at the beginning and end of the str1 string.
split(): By default, the string is split into a list with spaces, and when passed parameters, the string is split with the passed parameters.
Examples of these two functions:
upper and lower
The former converts every letter in the string to upper case, while the latter converts every letter to lower case:
__len__
This method is equivalent to Python 3's built-in len() function, which gets the number of characters in a string:
Single quotes, double quotes and three quotes
Strings in Python 3 can be marked with double quotes "" or single quotes ''; if only one pair of quotes appears, it makes no difference whether you use single quotes or double quotes.
If a string has both single and double quotes, use double quotes at the outermost and single quotes at the inside.
If only single quotes or double quotes occur multiple times in a string, the inner quotes need to be escaped with the escape character backslash\.
In Python, there are also cases where comments are multi-line, and multi-line comments are enclosed with three single quotes '' or three double quotes "". Comments multi-line situation generally appears at the beginning of the code file to explain the function of the entire file, use methods, precautions, or in the definition of the class below the class description and usage, can also be used in a function to explain its use or attention points. During program execution, comments in multiple lines are ignored by the program.
Thank you for reading! About "Python string how to use" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.