Article directory
String Common Operations
Common operations on strings include:
- Get length:
len
- Find what:
find
,index
,rfind
,rindex
- Judgment:
startswith
,endswith
,isalpha
,isdigit
,isalnum
,isspace
- Count occurrences:
count
- Replacement content:
replace
- Cut string:
split
,rsplit
,splitlines
,partition
,rpartition
- Modify case:
capitalize
,title
,upper
,lower
- Space handling:
ljust
,rjust
,center
,lstrip
,rstrip
,strip
- String concatenation:
join
Note: In Python, strings are immutable! All string-related methods will not change the original string, but return a result. In this new return value, the result after execution is retained!
len
len
function to get the length of a string.
mystr = '今天天气好晴朗,处处好风光呀好风光'
print(len(mystr)) # 17 获取字符串的长度
look up
Find related methods, which are used in roughly the same way, but with slight differences.
1. find
Find whether the specified content exists in the string, if it exists, return the starting position index value of the first occurrence of the content in the string, if not, return -1.
Grammar format:
S.find(sub[, start[, end]]) -> int
Example:
mystr = '今天天气好晴朗,处处好风光呀好风光'
print(mystr.find('好风光')) # 10 '好风光'第一次出现时,'好'所在的位置
print(mystr.find('你好')) # -1 '你好'不存在,返回 -1
print(mystr.find('风', 12)) # 15 从下标12开始查找'风',找到风所在的位置试15
print(mystr.find('风光',1,10)) # -1 从下标1开始到12查找"风光",未找到,返回 -1
2. rfind
Similar to find()
the function, but searches from the right.
mystr = '今天天气好晴朗,处处好风光呀好风光'
print(mystr.rfind('好')) # 14
3. index
Same as find()
the method, except that when the find method is not found, -1 is returned, and when str is not found, an exception will be reported.
Grammar format:
S.index(sub[, start[, end]]) -> int
4. rindex
Similar index()
, but starts from the right.
judgment
Python provides a very rich method that can be used to judge a string.
1. startswith
Determine whether the string starts with the specified content.
Grammar format:
S.startswith(prefix[, start[, end]]) -> bool
Example:
mystr = '今天天气好晴朗,处处好风光呀好风光'
print(mystr.startswith('今')) # True
print(mystr.startswith('今日')) # False
2. endswith
Determine whether the string ends with the specified content.
mystr = '今天天气好晴朗,处处好风光呀好风光"
print(mystr.endswith("风光")) # True
print(mystr.endswith("好风光")) # False
#### 3. isalpha
判断字符串是否全是字母字符。
```python
mystr = 'Hello'
print(mystr.isalpha()) # True
mystr = 'Hello123'
print(mystr.isalpha()) # False
4. also
Determines whether a string is full of numeric characters.
mystr = '12345'
print(mystr.isdigit()) # True
mystr = '123abc'
print(mystr.isdigit()) # False
5. the ice room
Determines whether a string is all alpha or numeric characters.
mystr = 'Hello123'
print(mystr.isalnum()) # True
mystr = 'Hello!123'
print(mystr.isalnum()) # False
6. isspace
Determines whether a string is full of space characters.
mystr = ' '
print(mystr.isspace()) # True
mystr = ' Hello '
print(mystr.isspace()) # False
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model