当前位置:   article > 正文

python输出字符串的几种方式_python打印字符串

python打印字符串

使用 print() 函数

这是最基础和常用的输出方式。它可以直接输出字符串,也可以输出其他数据类型,如数字、列表等。

name = "Alice"
age = 25
print(name)  # Output: Alice
print("My name is", name, "and I'm", age, "years old.")
# Output: My name is Alice and I'm 25 years old.
  • 1
  • 2
  • 3
  • 4
  • 5

使用 f-strings (格式化字符串字面量)

f-strings 是 Python 3.6 引入的一种新的字符串格式化方式,它使用 f 或 F 前缀来标识格式化字符串。f-strings 可以直接在字符串中嵌入表达式,并将其结果插入到输出中。

基本用法

f-strings 使用 f 或 F 前缀来标识格式化字符串,然后可以在字符串中直接使用变量或表达式,它们会被自动替换为相应的值。


name = "Alice"
age = 25
print(f"My name is {name} and I'm {age} years old.")
# Output: My name is Alice and I'm 25 years old.
  • 1
  • 2
  • 3
  • 4
  • 5
'
运行

使用表达式

在 f-strings 中,你可以使用任意有效的 Python 表达式,它们会被自动计算并替换到输出中。

radius = 5
area = 3.14 * radius ** 2
print(f"The area of a circle with radius {radius} is {area:.2f} square units.")
# Output: The area of a circle with radius 5 is 78.54 square units.
  • 1
  • 2
  • 3
  • 4

调用方法

你可以在 f-strings 中调用对象的方法,并使用它们的返回值。

name = "alice"
print(f"My name is {name.capitalize()}.")
# Output: My name is Alice.
  • 1
  • 2
  • 3

使用格式化选项

f-strings 支持与 str.format() 方法类似的格式化选项,例如对齐、填充、精度等。

number = 123.456789
print(f"The number is {number:10.2f}")
# Output: The number is     123.46
  • 1
  • 2
  • 3

嵌套大括号

如果需要在 f-strings 中使用大括号 {},可以通过双重大括号来转义它们。

template = "My name is {name} and I'm {age} years old."
print(f"The template is: {{template}}")
# Output: The template is: {template}
使用字典和其他数据结构:
f-strings 可以方便地访问字典、列表等数据结构中的元素。
python
复制
person = {"name": "Alice", "age": 25}
print(f"My name is {person['name']} and I'm {person['age']} years old.")
# Output: My name is Alice and I'm 25 years old.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

使用 str.format() 方法

str.format() 方法提供了更强大的字符串格式化功能。它使用大括号 {} 作为占位符,并在后面的参数中提供相应的值。

name = "Alice"
age = 25
print("My name is {} and I'm {} years old.".format(name, age))
# Output: My name is Alice and I'm 25 years old.

# 使用命名参数
print("My name is {name} and I'm {age} years old.".format(name=name, age=age))
# Output: My name is Alice and I'm 25 years old.

# 使用索引参数
print("My name is {0} and I'm {1} years old.".format(name, age))
# Output: My name is Alice and I'm 25 years old.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

使用 % 操作符 (Python 2.x 风格)

这是 Python 2.x 中常用的字符串格式化方式,虽然在 Python 3.x 中仍然可用,但已经被更现代的方式所取代。

name = "Alice"
age = 25
print("My name is %s and I'm %d years old." % (name, age))
# Output: My name is Alice and I'm 25 years old.

# 使用更复杂的格式化选项
PI = 3.14159
print("The value of pi is approximately %.2f." % PI)
# Output: The value of pi is approximately 3.14.
### 使用字符串拼接
这是最基础的字符串输出方式,适用于简单的字符串拼接。当字符串较长时,可能会降低代码的可读性。
```py
name = "Alice"
age = 25
print("My name is " + name + " and I'm " + str(age) + " years old.")
# Output: My name is Alice and I'm 25 years old.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

优缺点

print() 函数是最简单直接的输出方式,适用于简单的字符串输出。

f-strings是 Python 3.6 引入的新的字符串格式化方式,更加简洁和易读,可以在字符串中嵌入表达式。

str.format() 方法提供了更强大的格式化功能,可以应对更复杂的情况,支持命名参数和索引参数。

% 操作符是 Python 2.x 中常用的字符串格式化方式,在 Python 3.x 中虽然仍然可用,但已经被更现代的方式所取代。

字符串拼接适用于简单的字符串拼接,但当字符串较长时可能会降低代码的可读性。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/953409
推荐阅读
相关标签
  

闽ICP备14008679号