例举一下常规用法
print('字符串定义')
str1 = 'str'
str2 = "str"
# 声明段落
str3 = '''
paragraph
'''
# 使用续行符
str4 = "line1\
line2"
print('字符串拼接')
ret = str1 + ' ' + str2
print(ret) # print:Hello world
age = 22
score = 89.5
ret = f"age:{age},score:{score}"
a = {"aa", "bb"}
print(a)
print(",".join(a)) # print:aa,bb;也有可能 bb,aa
a = ["aa", "bb"]
print(",".join(a)) # print:aa,bb
a = {"name": "test", "sex": "nan"}
print(",".join(a)) # print:name,sex
print('截断字符串')
s = "0123456789"
# find(substring, start, end)
print(s.find('89'))
print(s.split('5'))
# s[0] = 1 赋值操作,代码不会报错,但是执行会报错
print(len(s)) # print: 13
print(s[0]) # print: 0
print(s[7]) # print: 7
print(s[-1]) # print: 9(倒数第一位)
# 区间截断,区间左闭右开
print(s[3:8]) # print: 34567(3-8 区间)
print(s[7:]) # print: 789(7 至结尾)
print(s[:5]) # print: Hello(0-5 区间)
print(s[3:8:3]) # print: 36(3-8 区间,每 3 位打印 1 次,包含第 0 位)
print(s[::-1]) # print: 9876543210(反转字符串)
print('hello world!!')
标签:aa,bb,python,ret,print,str,字符串
From: https://www.cnblogs.com/chenss15060100790/p/18581185