Article directory
learning target
- String representation
- Subscripting and slicing of strings
- Common operations on strings
- Character sets and encodings
- member operator
- format method of string
Introduction to strings
A string can be understood as a piece of ordinary text content. In python, quotation marks are used to represent a string, and different quotation marks have different effects.
string representation
a = "I'm Tom" # 一对双引号
b = 'Tom said:"I am Tom"' # 一对单引号
c = 'Tom said:"I\'m Tom"' # 转义字符
d = '''Tom said:"I'm Tom"''' # 三个单引号
e = """Tom said:"I'm Tom" """ # 三个双引号
small summary
- The data in double quotes or single quotes is a string
- If you use a pair of quotes to define the string, you can use escape characters when there is a symbol conflict
- A string defined with three single quotes and double quotes can wrap any text
escape character
Escape characters are part of the formal grammars of many programming languages, data formats, and communication protocols. Use \ to represent an escape character. Common escape characters and their meanings are as follows:
escape character | meaning |
---|---|
\r | Move the current position to the beginning of the line |
\n | Move the current position to the beginning of the next line |
\t | used to represent a tab character |
\ | represents a backslash character |
’ | used to display a single quote |
" | used to display a double quote |
Subscripting and slicing
1. Subscript/Index
The so-called "subscript" is also called "index", which is the number, just like the number of the storage cabinet in the supermarket, through which the corresponding storage space can be found
"Subscripts" in life
supermarket locker
The use of "subscript" in the string
If there is a string: name = 'abcdef', the actual storage in memory is as follows:
1. If you want to take out some characters, you can use the subscript method, (note that in the computer, the subscript starts from 0)
name = 'abcdef'
print(name[0])
print(name[1])
print(name[2])
operation result:
a
b
c
2. traverse
The so-called traversal can be understood as accessing each piece of data according to certain rules (usually the subscript of the data). Not all data can be traversed, and strings are iterable objects that can be traversed.
You can use while and for statements to iterate over the elements in a string.
The while statement traverses:
msg = 'hello world'
i = 0
while i < len(msg):
print(msg[i])
i += 1
The for statement traverses:
msg = 'hello world'
for x in msg:
print(x)
3. Slicing
Slicing refers to the operation of intercepting a part of the object being operated on. Strings, lists, and tuples all support slice operations.
The syntax of slicing: [start: end: step size] can also be simplified using [start: end]
Note: The selected interval starts from the "start" bit and ends with the previous bit of the "end" bit (not including the end bit itself), and the step length indicates the selection interval.
# 索引是通过下标取某一个元素
# 切片是通过下标去某一段元素
s = 'Hello World!'
print(s)
print(s[4]) # o 字符串里的第4个元素
print(s[3:7]) # lo W 包含下标 3,不含下标 7
print(s[:]) # Hello World! 取出所有元素(没有起始位和结束位之分),默认步长为1
print(s[1:]) # ello World! 从下标为1开始,取出 后面所有的元素(没有结束位)
print(s[:4]) # Hell 从起始位置开始,取到 下标为4的前一个元素(不包括结束位本身)
print(s[:-1]) # Hello World 从起始位置开始,取到 倒数第一个元素(不包括结束位本身)
print(s[-4:-1]) # rld 从倒数第4个元素开始,取到 倒数第1个元素(不包括结束位本身)
print(s[1:5:2]) # el 从下标为1开始,取到下标为5的前一个元素,步长为2(不包括结束位本身)
print(s[7:2:-1]) # ow ol 从下标为7的元素开始(包含下标为7的元素),倒着取到下标为2的元素(不包括下标为2的元素)
# python 字符串快速逆置
print(s[::-1]) # !dlroW olleH 从后向前,按步长为1进行取值
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