1、换行、转义与+
str_a = "abs"
print(str_a)
print(type(str_a))
str_b = "abstest@###\n2487376"
#\n表示换行
print(str_b)
str_c = "abstest@###\\n2487376"
#在/n前再加一个/代表成功打印出\n
print(str_c)
str_d = r"abstest@###\n2487376"
#在双引号的前面加一个r,也能实现将\n打印出来
print(str_d)
str_e = '12233444'
str_f = "qefhsghgfr"
#加号的使用
print(str_e+str_f)
2、字符串格式化字符
print("hogwarts teacher is %s"%"Me")
print("hogwarts teacher is %s"%"111")
print("hogwarts teacher is %d"%111)
print("hogwarts teacher is %f"%2.2)
#输出
#hogwarts teacher is Me
#hogwarts teacher is 111
#hogwarts teacher is 111
#hogwarts teacher is 2.200000
3、format的使用
demo = "hogwarts is a {}"
print(demo)
# demo是原始的变量内容
demo_res = demo.format("school")
print(demo_res)
# demo_res是替换过后的变量内容
demo = "hogwarts is a {0} {1}"
# 第一个参数放在0的位置,第二个参数放在1的位置
demo_res = demo.format("very good","school")
print(demo_res)
demo = "hogwarts is a {school}"
demo_res = demo.format(school="very good school")
# 将"very good school"传给school
print(demo_res)
#输出
#hogwarts is a {}
#hogwarts is a school
#hogwarts is a very good school
#hogwarts is a very good school
4、 python3.6.5之后的语法:字符串前面添加f,变量使用{变量名}来表示
优点:比format更优雅更简洁
name = "AD"
print(f"my name is {name}")
#输出
#my name is AD
5、join的使用:把列表,根据想要的格式拼接成字符串
a = ["a","p","p","l","e"]
print("".join(a))
print("|".join(a))
#输出
#apple
#a|p|p|l|e
6、Split的使用,将字符串根据规定的内容进行切分。以什么内容进行切分,那么这个内容也会没有
b = "a|p|p|l|e"
print(b.split("|"))
b = "hogwarts school"
print(b.split("s"))
print(b.split(" "))
#输出
#['a', 'p', 'p', 'l', 'e']
#['hogwart', ' ', 'chool']
#['hogwarts', 'school']
7、 replace 将目标内容替换为想要替换的内容
c = "My name is ad"
print(c.replace("m","M"))
print(c.replace("ad","Shirley"))
#输出
#My naMe is ad
#My name is Shirley
8、 strip- 去除首尾空格
d = " My name is ad "
print(d)
#输出
# My name is ad (不会去除首尾空格)
d = " My name is ad "
print(d.strip())
#输出
#My name is ad(去掉了首尾空格)