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 introduces you to 12 Python data processing commonly used built-in functions are which, the content is very detailed, interested friends can refer to, I hope to help you.
In python data analysis, it is often necessary to perform various processing on strings, such as concatenating strings, retrieving strings, etc. Here I will introduce the built-in string manipulation functions commonly used in python.
1. Calculates the length of a string-len() function
str1='I love python' length=len(str1) print(length)
The len() function calculates the length of a string without distinguishing between English, numbers, and Chinese characters, and all characters are calculated as one character.
Sometimes, we need to get the actual bytes occupied by the string, i.e. calculate the actual byte length under python default encoding UTF-8, we can do this:
str1='I love python' length=len(str1.encode()) print(length)
12
And UTF-8 encoding under a Chinese character accounted for 3 bytes consistent
2. Split string
The split() method can be used to split a string, that is, to split a string into a list of strings according to the specified delimiter. The syntax is as follows
str.split(sep,maxsplit)
Parameter Description:
str: indicates the string to split sep: specified splitter maxsplit: used to specify the number of splits
3. Count the number of times a character appears
The count() method is used to retrieve the number of times a string occurs in another string. The syntax is as follows:
str.count(str1)
Parameter Description:
str: represents the original string str1: represents the string to retrieve
4. Retrieves whether a string exists (find)
The find() method is used to retrieve whether the specified substring is contained. Returns-1 if the retrieved string does not exist, otherwise returns the index of the first occurrence of the substring. The syntax is as follows:
str.find(str1)
Parameter Description:
str: indicates the original character str1: indicates the substring to retrieve
5. Retrieves whether a string exists (index)
index() is similar to the find() method, and is used to retrieve whether the specified substring is included. The index() method throws an exception if the specified string does not exist. The syntax is as follows:
str.index(str1)
Parameter Description:
str: indicates the original character str1: indicates the substring to retrieve
6. Retrieves whether a string has a specified string start
The startswith() method is used here. If yes, it returns True, otherwise it returns False. The specific syntax is as follows:
str.startswith(str1)
Parameter Description:
str: indicates the original character str1: indicates the substring to retrieve
7. Retrieves whether a string has a specified string end
The endswith() method is used here. If yes, it returns True, otherwise it returns False. The specific syntax is as follows:
str.endswith(str1)
Parameter Description:
str: indicates the original character str1: indicates the substring to retrieve
8. Capitals to lowercase
The lower() method is used to convert upper case letters to lower case letters in a price string. The syntax is as follows:
str.lower()
9. Conversion of lowercase letters to capital letters
The upper() method is used to convert lowercase letters in a price string to uppercase letters. The syntax is as follows:
str.upper()
10. Delete spaces and special characters
strip() method is used to remove spaces and special characters on the left and right sides of a string. The syntax is as follows:
str.strip([chars])
Parameter Description:
str: indicates that the original character chars: is an optional parameter, which is used to specify the character to be extracted. Multiple characters can be specified. If chars is "@," take out the "@" included on the left and right sides. "。If the chars parameter is not specified, spaces, tabs "\t", carriage returns "\r", newlines "\n", etc. are removed by default
11. Remove the characters from the left side of the string
The lstrip() method is used to remove spaces and special characters from the left side of a string. The syntax is as follows:
str.lstrip([chars])
Parameter Description:
str: indicates that the original character chars: is an optional parameter, which is used to specify the character to be extracted. Multiple characters can be specified. If chars is "@," take out the "@" included on the left and right sides. "。If the chars parameter is not specified, spaces, tabs "\t", carriage returns "\r", newlines "\n", etc. are removed by default
12. Remove the characters on the right side of the string
The rstrip() method is used to remove spaces and special characters from the right side of a string. The syntax is as follows:
str.rstrip()
Parameter Description:
str: indicates that the original character chars: is an optional parameter, which is used to specify the character to be extracted. Multiple characters can be specified. If chars is "@," take out the "@" included on the left and right sides. "。If you do not specify the chars parameter, the default removal of spaces, tabs "\t", carriage returns "\r", newline "\n" and so on about 12 Python data processing commonly used built-in functions are shared here, I hope the above content can have some help for everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can 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.