python 字符串

str 用合理的方式转换为字符串

print(str("hello,\nworld!"))

'hello,

world!'

repr 用合法的python表达式表示

print(repr("hello,\nworld!"))
'hello,\nworld!'

 

长字符串

要表示很长的字符串可以用三引号(字符串内的引号无需转义)


原始字符串

不以特殊的方式处理反斜杠,让字符串的每个字符保持原样,可在字符串中包含原本无法包含的字符

 

print('C:\nowProgram')

C:

owProgram

print(r'C:\nowProgram')

C:\nowProgram


Unicode bytes bytearray

Unicode 表示文本

每个Unicode字符都用一个码点表示,码点是Union标准给每个字符一个指定的数字。要使用时,用16位或32位的十六进制变量(加上前缀\u或\U)或用字符的Unicode名称\N{name}

"\uooC6"

"\Uooo1F60A"

"\N{cat}"

bytes(string,encoding="...")

将string用特定的编码方式进行编码

 

 

format 替换字符

(不用索引)

"{}.format(first)"

first

(用索引)

1.

"{1}{0}".format("first","second")

second first

2.

"{a}{b}".format(a="first",b="second")

first second

3.当变量与替换字段同名,可在字符串前加上f

f."e is{e}"

e is 2.718281828459045

等价

"e is {e}".format(e=e)

 

 

 

 

 

常规字符串也可跨多行,只需加上\

print("hello,\
world!")

hello,world!

 

\ 转义

\n 换行

%字符串格式设置运算符

%s转换说明符

 

posted on 2021-04-22 10:10  旺财爱吃肉  阅读(57)  评论(0)    收藏  举报

导航