Output:
Monday Tuesday WednesdayWednesday Tuesday MondayTuesday Wednesday MondayMonday Tuesday Wednesday
f-strings 更具可读性,并且它们比 str.format() 方法实现得更快 。因此,f-string 是字符串格式化的首选方法
处理引号和撇号
撇号 (') 在 Python 中表示一个字符串 。为了让 Python 知道我们不是在处理字符串,我们必须使用 Python 转义字符 () 。因此撇号在 Python 中表示为 ' 。与处理撇号不同,Python 中有很多处理引号的方法 。它们包括以下内容:
# 1. Represent string with single quote (`""`) and quoted statement with double quote (`""`)quotes_one = '"Friends don't let friends use minibatches larger than 32" - Yann LeCun'print(quotes_one)# 2. Represent string with double quote `("")` and quoted statement with escape and double quote `("statement")`quotes_two = ""Friends don't let friends use minibatches larger than 32" - Yann LeCun"print(quotes_two)# 3. Represent string with triple quote `("""""")` and quoted statment with double quote ("")quote_three = """"Friends don't let friends use minibatches larger than 32" - Yann LeCun"""print(quote_three)
Output:
"Friends don't let friends use minibatches larger than 32" - Yann LeCun"Friends don't let friends use minibatches larger than 32" - Yann LeCun"Friends don't let friends use minibatches larger than 32" - Yann LeCun
写在最后字符串作为编程语言当中最为常见的数据类型,熟练而灵活的掌握其各种属性和方法,实在是太重要了,小伙伴们千万要实时温习,处处留心哦!
推荐阅读
- Python函数科学速查表
- Django 入门:构建Python Web应用的全面指南
- Python函数详解:让代码更高效、易读、可维护
- AIGC+低代码,一场围绕开发的深度革命
- 到底该不该使用Python?
- Linux下Python安装配置指南
- Python 失宠!Hugging Face 用 Rust 新写了一个 ML 框架,现已低调开源
- Python编码的陷阱与避坑指南
- Python 3.11的10个高效新特性
- 深度NLP模型的泛化问题