Python Basics-04 Strings

String representation

  • In Python, you can use a pair of single quotes/double quotes or a pair of three double quotes/a pair of three single quotes to represent a string
a = 'hello'
b = "hello"
c = '''hello'''
d = """hello"""
# 如果字符串里面还有双引号,外面就可以使用单引号
# 反之一样
# 如果字符串里面有双引号,外层也可以使用一对三个双引号或者一对三个单引号
  • The escape character \ in a string
x = 'I\'m xiaoming' # \ 表示的是转义字符,作用是对 \ 后面的字符进行转义
# \' 显示一个普通的单引号
# \" 显示一个普通的双引号
# \n 表示一个换行
# \t 表示一个制表符
# \\ 表示一个普通的反斜线
# 在字符串引号前添加一个r,在Python中表示的是原生字符串

Subscripting and slicing of strings

  • Subscript we can also call it index, indicating the number of data
  • Iterable objects: str list tuple dict set range can be traversed
  • Among them, str list tuple can obtain or manipulate data through subscripts
  • In computers, subscripts start from 0
  • The data at the specified location can be obtained or modified through the subscript

String is an immutable data type
. Any operation on string will not modify the original string!!!
Slicing operation is to copy a specified content from a string to generate a new string

# 切片语法:
# m[start:end:step]
# 包含start,不包含end
# 如果只设置了start,会从start到结尾
m[1:]

# 如果只设置了end,会从开头到end
m[:4]

# step代表步长,每隔step-1取一次
m[2:5:1]

# 步长默认为1,步长不能为0

# 步长为负数的时候代表从右往左找,同时应注意start及end
m[15:3:-1]

# 可以使用::复制字符串
m[::]

# 可以使用::-1将字符串倒置
m[::-1]

# start和end如果是负数,但step不为负数时,相当于从右往左找找到对应的start和end(右往左第一个是-1),但字符串输出还是从左往右
a = 'abcdefghijklmnopqrstuvwxyz'
print(a[-9:-5])
# rstu

print(a[-5:-9:-1])
# vuts

Common operations on strings

a = 'abcdefghijklmnopqrstuvwxyzi'
# 获取字符串的长度
print(len(a))  # 27

# 查找内容相关的方法

# find/index/rfind/rindex 可以获取指定字符的下标
print(a.find('i'))  # 8,如果字符存在,返回下标,不存在返回-1
print(a.index('i'))  # 8,如果字符存在,返回下标,不存在就报错
print(a.rfind('i'))  # 26,从右往左查找第一个指定字符(最大索引),不存在返回-1
print(a.rindex('i'))  # 26,从右往左查找第一个指定字符(最大索引),不存在报错

# 判断相关的方法

# startswitch,endswitch,isalpha,isdigit,isalnum,isspace
# is开头的是判断,结果是一个布尔类型
# 均为字符串的使用方法
# startswitch  以某个字符开始
# endswitch  以某个字符结束
# isalpha  是否是字母
# isdigit  是否是数字
# isalnum  是否是数字加字母,只写数字或者只写字母都是True
# isspace  是否全部是空格

# 字符串替换方法

# replace
word = 'hello'
print(word.replace('l','x'))  # 原来的字符串不会变,而是生成一个新的字符串用来保存替换后的结果

Operations related to string content segmentation

split rsplit splitlines partition rpartition

x = 'zhangwan-lisi-wangwu-ermazi-dazhutou'
# 使用split方法,可以将一个字符串切割成一个列表,可以传两个参数,第一个是分隔的字符,第二个是分隔的次数
print(x.split('-'))  # ['zhangwan', 'lisi', 'wangwu', 'ermazi', 'dazhutou']
print(x.split('-',2))  # ['zhangwan', 'lisi', 'wangwu-ermazi-dazhutou']


# 使用rsplit方法,可以将一个字符串切割成一个列表,传第二个参数代表从右往左分几次
print(x.rsplit('-'))  # ['zhangwan', 'lisi', 'wangwu', 'ermazi', 'dazhutou']
print(x.rsplit('-',2))  # ['zhangwan-lisi-wangwu', 'ermazi', 'dazhutou']

# 使用splitlines,可以将一个字符串按照换行符进行分隔,返回一个列表
a = 'hello \n world'
print(a.splitlines())  # ['hello ', ' world']

# 使用partition,可以按照传入的参数从左往右查找第一个匹配的参数,将字符串分为三个部分,返回一个元组
x = 'zhangwan-lisi-wangwu-lisi-ermazi-dazhutou'
print(x.partition("lisi"))  # ('zhangwan-', 'lisi', '-wangwu-lisi-ermazi-dazhutou')

# 使用rpartition可以按照传入的参数从右往左查找第一个匹配的参数,将字符串分为三个部分,返回一个元组
print(x.rpartition("lisi"))  # ('zhangwan-lisi-wangwu-', 'lisi', '-ermazi-dazhutou')

character encoding

  • The ASCII code table uses a byte to represent a character. It can only represent up to 128 characters, and the highest bit is not applicable, 0-127
  • ISO-8859-1 uses the highest bit 0-127, which is fully compatible with the ASCII code table, and can represent up to 255 characters, 0-254
  • Unicode encoding --> Most of the country's problems have a corresponding encoding

encoding set for strings

Use the built-in functions chr and ord to view the correspondence between numbers and characters
ord to get the code corresponding to the character
chr get the corresponding character according to the code

print(ord('A'))  # 65
print(chr(65))  #A
  • the result of converting the string to the specified encoding set
  • If there is a result of a code set, and you want to convert it to the corresponding character, use decode
# 字符串转换为指定的编码集
a = '你'
print(a.encode('gbk'))
print(a.encode('utf-8'))

# b'\xc4\xe3'
# b'\xe4\xbd\xa0'

# 编码结果转换为对应的字符
x = b'\xe4\xbd\xa0'
print(x.decode('utf-8'))

# 你

member operator

The priority of in and not in operators
is used to determine whether a content exists in an iterable object

word = "hello"
c = input("请输入一个字符")

for x in word:
    if x == c :
        print('你输入的内容存在')
        break
else:
    print('你输入的内容不存在')

formatted print string

  • A string can be formatted using the % placeholder
  • %s represents a placeholder for a string
  • %d represents a placeholder for an integer
    • %nd When printing, display n digits, if not enough, fill it with spaces in front
  • %f represents a placeholder for floating point numbers
    • %.nf keep n digits after the decimal point when printing
  • %x output the number in hexadecimal
  • %% output a %
name = "zhangsan"
age = 18
print('我叫%s,今年%d岁了'%(name,age))

# 我叫zhangsan,今年18岁了

# %d 拓展用法
print('今天是第%3d天'%(5))
print('今天是第%03d天'%(5))
print('今天是第%-3d天'%(5))

# 今天是第  5天
# 今天是第005天
# 今天是第5  天

# %f 拓展用法
print('我有%.2f元钱'% 3.1415926)

# 我有3.14元钱

The use of the format method of the string

  • {} can also be used for string placeholders
# {}中什么都不写,会读取format方法后面的内容,一一对应填充
x = '大家好,我是{},今年{}岁了'.format('zhangsan',18)
print(x)  # 大家好,我是zhangsan,今年18岁了

# {数字}会根据数字的顺序读取format方法后面的内容,按顺序对应,数字从0开始
x = '大家好,我是{1},今年{0}岁了'.format(18,'zhangsan')
print(x)  # 大家好,我是zhangsan,今年18岁了

# {变量}会根据变量读取format方法后面的内容,一一对应填充
x = '大家好,我是{name},今年{age}岁了'.format(age = 18,name = 'zhangsan')
print(x)  # 大家好,我是zhangsan,今年18岁了

# {数字}{变量}可以混合使用,但是需要一一对应
# {}和{数字}不可以混合使用

# 列表配合format使用
d = ['zhangsan',18]
x = '大家好,我是{},今年{}岁了'.format(*d)
print(x)  # 大家好,我是zhangsan,今年18岁了

#字典配合format使用
d = {
    
    'name':'zhangsan','age':'18'}
x = '大家好,我是{name},今年{age}岁了'.format(**d)
print(x)  # 大家好,我是zhangsan,今年18岁了

Guess you like

Origin blog.csdn.net/Lz__Heng/article/details/130124781