玻璃行业做的非常有设计感的网站,中国庆阳网,asp网站开发的主要困难,如何设置网站布局1. 原始字符串 (Raw String) - r 或 R
使用 r 或 R 前缀#xff0c;可以告诉 Python 字符串中的所有反斜杠都是普通字符#xff0c;而不是转义字符。这在处理文件路径、正则表达式等情况下非常有用。
path rC:\new_folder\test.txt # 原始字符串2. 格式化字符串 (Formatt…1. 原始字符串 (Raw String) - r 或 R
使用 r 或 R 前缀可以告诉 Python 字符串中的所有反斜杠都是普通字符而不是转义字符。这在处理文件路径、正则表达式等情况下非常有用。
path rC:\new_folder\test.txt # 原始字符串2. 格式化字符串 (Formatted String) - f 或 F
使用 f 或 F 前缀可以在字符串中嵌入表达式。这些表达式在运行时会被计算并将结果插入到字符串中。这种字符串被称为 f-string是在 Python 3.6 引入的。
name Alice
age 30
message f{name} is {age} years old. # 格式化字符串3. Unicode 字符串 - u 或 U
在 Python 3 中所有字符串默认都是 Unicode因此 u 前缀通常不再需要。但是在 Python 2 中它用于创建 Unicode 字符串。
# 在 Python 3 中
text uHello, world! # Unicode 字符串# 在 Python 2 中
text uHello, world! # Unicode 字符串4. 字节字符串 (Byte String) - b 或 B
使用 b 或 B 前缀来创建字节字符串而不是文本字符串。字节字符串用于处理二进制数据常用于文件 I/O 和网络传输。
data bHello, world! # 字节字符串5. 三重引号 (Triple Quotes)
三重引号可以用于定义跨多行的字符串。这种字符串可以用三重单引号 () 或三重双引号 () 定义。
multiline_str This is a
multiline string that spans
multiple lines.6. 组合使用修饰符
可以组合使用字符串修饰符。例如既要使用原始字符串又要进行格式化
path rC:\new_folder\test.txt
name Alice
message fr{name}\s file is located at {path}
print(message)
# Output: Alices file is located at C:\new_folder\test.txt示例代码
# 使用原始字符串
raw_path rC:\Users\Example\Documents\file.txt
print(raw_path)# 使用格式化字符串
name John
age 28
greeting fHello, {name}. You are {age} years old.
print(greeting)# 使用 Unicode 字符串
unicode_str uこんにちは世界 # 这在 Python 3 中默认就是 Unicode
print(unicode_str)# 使用字节字符串
byte_str bThis is a byte string
print(byte_str)# 使用多行字符串
multiline_str This is a string
that spans multiple
lines.
print(multiline_str)# 组合使用原始和格式化字符串
file_path rC:\Users\Example\Documents
filename file.txt
full_path fr{file_path}\{filename}
print(full_path)