python3数据类型--字符串

xiaoxiao2025-08-21  53

写在最前面:写点基础吧,骗一点访问量,以下均基于python3

String,str = 'string’或者"string",那么字符串用单引号和双引号有什么区别呢?

如果你的字符串里只带’’

str = "abc'de'"

如果你的字符串里只带’’

str = "abc'd'"

如果你的字符串同时带’’,""

str = 'abc"eee"\'ddd\'"ddd""

字符串三引号:

三个单引号:

str = '''i love you''' print(str) i love you

三个双引号:

str = """i love you""" print(str) i love you

其实效果是一样的,主要的好处是不用\n转义换行了,不然就得这样

str = "i \nlove \nyou" print(str) i love you

更新字符串:

str = "This is an apple'" print("更新字符串 :", str[:9] + ' orange') 更新字符串 : This is a orange

字符串输出

print(str) # 输出完整字符串 print(str[0]) # 输出字符串中的第一个字符 print(str[1:5]) # 输出字符串中第三个至第五个之间的字符串 print(str[2:]) # 输出从第三个字符开始的字符串 print(str * 2) # 输出字符串两次 print(str + " TEST") # 输出连接的字符串 This is an apple T his is is an apple This is an appleThis is an apple This is an apple TEST

字符串连接的三种方式: 使用 " + "

str = 'I ' + 'like' + ' apple' print(str) I like apple

使用 join

liststr = ['I', 'like', 'apple'] str = ' '.join(liststr) print(str) I like apple

也可以这么用

newseq = ("I","like","apple") seq =" " str = seq.join(newseq) print(str) I like apple

简直随心所欲有木有

字符串格式化

print("I like %s and I want %d !" % ('apple', 5)) I like apple and I want 5 !

%s 格式化字符串 %d 格式化整数 %c 格式化字符及其ASCII码 %u 格式化无符号整型

这是几个常用的

基础就写这么多,欢迎指正,原创不易,希望大家多多关注~

转载请注明原文地址: https://www.6miu.com/read-5035064.html

最新回复(0)