[Python] basic data type - string

1. Definition

Single-line strings: use '…' or "…"

Multi-line strings: use '''...'''

"字符串"
'字符串'

'''
多行字符
多行字符
'''

2. Commonly used special characters

\:Escapes. \n: Newline

r: Ignore the role of the escape character. Used at the beginning of a string, the internal "\" no longer acts as an escape character.

+: multiple string concatenation.

print('a\nb')         # a
                      # b
print(r'a\nb')        # a\nb

print('ab' + 'cd')    # abcd

3. Literal interpolation

Literal value: the original value given by variable or constant, the literal value can be directly used in the program.

Literal interpolation: A technique for interpolating variables, constants, and expressions to avoid string concatenation problems.

1. Formatted output

Writing: "%x" % (x)

  • %c formatting characters and their ASCII codes
  • %s format string
  • %d formatted integer
  • %u format unsigned integer
  • %o format unsigned octal number
  • %x format unsigned hexadecimal number
  • %X format unsigned hexadecimal number (uppercase)
  • %f Format floating-point numbers, you can specify the precision after the decimal point
  • %e format floating point numbers in scientific notation
  • %p format the address of the variable with a hexadecimal number
print('小明第%d次考试,考了%.2f,等级%s' % (2, 92.5, 'A'))
# 输出:小明第2次考试,考了92.50,等级A
# %.2f:代表保留两位小数

2、format()

Writing method: "{}".format(x)

# 不指定位置,按默认顺序
print("{} name is {}".format("My", "Andy"))
# 指定位置
print("{1} name is {0}".format("Andy", "My"))
# 通过名称传递变量
print("{} name is {name}".format("My", name = "Andy"))

# 均是输出:My name is Andy

3、f

Writing: f"{variable}"

name = "Andy"
print(f"My name is {name}")    # 输出:My name is Andy

4. Common APIs 

1、join()

convert list to string

Writing method: " ".join(list), the character in " " means to connect with this character.

a = ["My", "name", "is", "Andy"]
print(' '.join(a))    # 输出:My name is Andy
print('|'.join(a))    # 输出:My|name|is|Andy

2、split()

Data Slicing Operation

Writing method: str. split(" "). The characters inside " " means splitting with this character.

s = "My name is Andy"
print(s.split(" "))    # 输出:['My', 'name', 'is', 'Andy']
print(s.split("a"))    # 输出:['My n', 'me is Andy']

3、replace()

replace target string

Writing method: str.replace(" ", " "). The previous "" is the string to be replaced, and the next "" is the string to be replaced.

s = "My name is Andy"
print(s.replace("Andy", "Kim"))    # 输出:My name is Kim

4、strip() 

Remove leading and trailing spaces from a string

Writing method: str. strip().

s = " My name is Andy "
print(s.strip())    # 输出:My name is Andy

lstrip(): remove the head space, left abbreviation; rstrip(): remove the trailing space, right abbreviation

s = " My name is Andy "
print(s.lstrip())    # 输出:My name is Andy
print(s.rstrip())    # 输出: My name is Andy

 5、find()

Find the position of the first occurrence of a substring in a string, and return the index.

Writing method: str. find(" "). " "Write the substring to be searched. Returns -1 if not found.

s = "My name is Andy"
print(s.find("am"))    # 输出:4
print(s.find("aw"))    # 输出:-1

6、upper()、lower()、title()、capitalize()

  • upper(): Convert all letters in the string to uppercase letters
  • lower(): Convert all letters in the string to lowercase letters
  • title(): Convert the first letter of each word in the string to uppercase, and the rest to lowercase 
  • capitalize(): Convert the first letter of the string to uppercase, and the rest to lowercase

写法: str.upper()、str.lower()、str.title()、str.capitalize()

s = "My name is Andy"
print(s.upper())        # 输出:MY NAME IS ANDY
print(s.lower())        # 输出:my name is andy
print(s.title())        # 输出:My Name Is Andy
print(s.capitalize())   # 输出:My name is andy

Guess you like

Origin blog.csdn.net/Yocczy/article/details/131819255