The most commonly used data type in Python-string

A string is a string of characters, a data type that represents text in a programming language.

Strings are the most commonly used data type in Python.

String declaration

You can use a pair of double quotes "or a pair of single quotes to 'define a string in Python .

So when developing, should we use double quotes or single quotes?

First of all, most programming languages ​​use double quotation marks to define strings, so we prefer to use double quotation marks when developing, so that it can communicate with other programming languages.

Although you can use \"or \'do string escaping, in actual development:
if double quotation marks are needed inside the string, then single quotation marks can be used to define the string.
If you need to use single quotes inside the string, you can use double quotes to define the string.

str1 = '我的外号是"大西瓜"'
print(str1)
# 我的外号是"大西瓜"

Triple quotes to declare string variables

There is also a special case, when the string to be declared contains newline characters, tab characters, and other special characters, we can use triple quotation marks to assign values.

Variables declared with three quotes in Python are for complex strings.

The syntax of triple quotation marks is a pair of consecutive single quotation marks or double quotation marks (usually used in pairs).

hi = '''hi 
there'''
print(hi)
# hi 
# there

The triple quotation marks declare that the string can maintain a small piece of string format throughout the so-called WYSIWYG format.

A typical use case is when you need a piece of HTML or SQL, it is very convenient to mark with triple quotes, and it will be very troublesome to use the traditional escape character system.

String index concept

Previously, the ancestor of the learning list has the concept of index. We can also think of a string as a large cabinet that stores multiple characters, and stores one character after another in each small grid.

So we can also use the index to get the character at a specified position in a string, and the index count also starts from 0.

String index diagram

str1 = "hello python"
print(str1[6])
# p

We can also use the for loop to read each character in the string by iterative traversal.

Instance

string = "Hello Python"
for char in string:
    print(c)

Common operations on strings

Python provides a lot of string manipulation methods, but it is precisely because Python provides enough methods that it enables more flexible operations on strings during development to meet more development needs!

Only a few commonly used string methods are introduced here, and the rest can be read through the manual when they are used in actual development.

len statistical string length

hello_str = "hello hello"
print(len(hello_str))
# 11

count counts the number of times a small (sub) string appears

hello_str = "hello hello"
print(hello_str.count("llo"))
print(hello_str.count("abc"))
# 2
# 0 子串不存在时,程序不报错。

isspace judges blank characters.

space_str = "      \t\n\r"
print(space_str.isspace())
# True

Determine whether the string contains only numbers.

num_str = "1.1"
# 都不能判断小数
print(num_str.isdecimal()) # False
print(num_str.isdigit()) # False
print(num_str.isnumeric()) # False
# unicode 字符串
num_str = "\u00b2"
print(num_str.isdecimal()) # False
print(num_str.isdigit()) # True
print(num_str.isnumeric()) # True
# 中文数字
num_str = "一千零一"
print(num_str.isdecimal()) # False
print(num_str.isdigit()) # False
print(num_str.isnumeric()) # True

startswith determines whether to start with the specified string

hello_str = "hello world"
print(hello_str.startswith("Hello"))
# False

endswith judge whether to end with the specified string

hello_str = "hello world"
print(hello_str.endswith("world"))
# True

index Find the position where a certain substring appears

hello_str = "hello hello"
print(hello_str.index("llo"))
# 2
# 使用index方法传递的子字符串不存在,程序会报错
print(hello_str.index("abc"))
# ValueError: substring not found

find Find the position where a certain substring appears

hello_str = "hello world"
print(hello_str.find("llo"))
# 2
# find如果指定的字符串不存在,会返回-1
print(hello_str.find("abc"))
# -1

replace replace string

hello_str = "hello world"
print(hello_str.replace("world", "python"))
# hello python
print(hello_str)
# hello world

After the replace method is executed, it will return a new string without modifying the content of the original string, so a variable is needed to receive it.

strip removes extra spaces and special characters in the string

str = "  www.daydaylearn.cn \t\n\r"
print(str)
str2 = str.strip()
# 并不会改变字符串本身,需要新的变量接收。
print(str2)
#  www.daydaylearn.cn

#www.daydaylearn.cn

In str.strip([chars]), [chars] is used to specify the characters to be deleted. You can specify multiple characters at the same time. If you do not specify them manually, the default will delete spaces, tabs, carriage returns, line feeds, etc. Special characters.

split split string

poem_str = "登鹳雀楼\t 王之涣 \t 白日依山尽 \t \n 黄河入海流 \t\t 欲穷千里目 \t\t\n更上一层楼"
poem_list = poem_str.split()
print(poem_list)
# ['登鹳雀楼', '王之涣', '白日依山尽', '黄河入海流', '欲穷千里目', '更上一层楼']

join to merge strings

poem_list = ['登鹳雀楼', '王之涣', '白日依山尽', '黄河入海流', '欲穷千里目', '更上一层楼']

result = " ".join(poem_list)
print(result)
# 登鹳雀楼 王之涣 白日依山尽 黄河入海流 欲穷千里目 更上一层楼

The pilgrimage of programming

Guess you like

Origin blog.csdn.net/beyondamos/article/details/108054159