在 Python 中打印变量之间没有空格
使用格式化的字符串文字来打印没有空格的变量,例如 print(f'hello {variable_1}!')
。 格式化字符串文字(f-字符串)让我们通过在字符串前加上 f
来在字符串中包含表达式和变量。
variable_1 = 'world'
# ✅ 使用格式化字符串文字打印不带空格的值
print(f'hello {variable_1}!') # 👉️ 'hello world!'
# ------------------------------------------
# ✅ 使用加法 (+) 运算符打印不带空格的值
print('hello ' + variable_1 + '!') # 👉️ 'hello world!'
# ------------------------------------------
# ✅ 使用 sep 参数打印不带空格的值
print('hello ', variable_1, '!', sep='') # 👉️ 'hello world!'
第一个示例使用格式化字符串文字来打印值,它们之间没有空格。
variable_1 = 'world'
print(f'hello {variable_1}!') # 👉️ 'hello world!'
格式化字符串文字 (f-strings) 让我们通过在字符串前加上 f
来在字符串中包含表达式。
my_str = 'is subscribed:'
my_bool = True
result = f'{my_str} {my_bool}'
print(result) # 👉️ 'is subscribed: True'
确保将表达式用大括号括起来 - {expression}
。
如果我们需要从字符串中删除前导和尾随空格,请使用
str.strip()
方法。
my_str = ' hello '
result = my_str.strip()
print(repr(result)) # 👉️ 'hello'
str.strip
方法返回删除了前导和尾随空格的字符串副本。
str.strip
方法不会更改原始字符串,它会返回一个新字符串。 字符串在 Python 中是不可变的。
我们可以直接在格式化字符串文字中调用 str.strip()
方法。
variable_1 = ' world '
print(f'hello {variable_1.strip()}!') # 👉️ 'hello world!'
或者,我们可以使用 sep
关键字参数。
使用 sep 参数打印变量之间没有空格
使用 print()
函数的 sep 参数打印它们之间没有空格的值,例如 print('hello ', variable_1, '!', sep='')
。 可以将 sep 参数设置为空字符串以打印值之间没有空格。
variable_1 = 'world'
print('hello ', variable_1, '!', sep='') # 👉️ 'hello world!'
如果在使用 print()
函数时需要删除多余的空格,请将 sep 关键字参数设置为空字符串。
variable = 'hello '
# 👇️ hello world!
print(variable, 'world', '!', sep='')
sep
关键字参数是值之间的分隔符。
默认情况下,sep
参数设置为空格。
通过将参数设置为空字符串,不会在值之间添加额外的空格。
或者,我们可以使用加法 +
运算符。
使用加法 (+) 运算符打印不带空格的变量
使用加法运算符打印不带空格的变量,例如 print('hello ' + variable_1 + '!')
。 加法 +
运算符将打印变量而不在它们之间添加额外的空格。
variable_1 = 'world'
print('hello ' + variable_1 + '!') # 👉️ 'hello world!'
加法 +
运算符连接值而不在它们之间添加额外的空格。
但是,请注意左侧和右侧的值必须是兼容的类型。
如果我们尝试连接不兼容类型的值,则会出现错误。
# ⛔️ TypeError: can only concatenate str (not "int") to str
print('abc' + 123)
要解决这个问题,我们必须转换其中一个值。
print('abc' + str(123)) # 👉️ 'abc123'
使用格式化字符串文字时,我们不必确保值的类型兼容,因为 f-strings
会自动为我们将值转换为字符串。
在 Python 中不使用换行符或空格打印
使用 print()
函数的 end 和 sep 参数在没有换行符或空格的情况下打印。
end 参数可以设置为空字符串以在没有换行符的情况下打印,sep 参数可以设置为空字符串以在值之间不带空格的情况下打印。
# ✅ print without newline
print('jiyik', end='') # 👉️ jiyik
# ✅ print without spaces
print('fql', 'jiyik', '.com', sep='') # 👉️ fqljiyik.com
# ✅ print without newline and spaces
print('fql', 'jiyik', sep='', end='') # 👉️ fqljiyik
第一个示例使用 end
参数在没有换行符 \n
的情况下进行打印。
end
参数打印在消息的末尾。
默认情况下,结束设置为换行符 \n
。
for item in ['fql', 'jiyik']:
# fql
# jiyik
print(item)
for item in ['fql', 'jiyik']:
# fql jiyik
print(item, end=' ')
我们可以将结束参数设置为空字符串或包含空格的字符串。
传递给
print()
函数的消息后,会立即打印结束参数的值。
sep 参数是我们传递给 print()
的参数之间的分隔符。
print('fql', 'jiyik', sep='') # 👉️ fqljiyik
print('fql', 'jiyik') # 👉️ fql jiyik
默认情况下,sep 参数设置为空格。
我们可以将其设置为空字符串,以在值之间不带空格地打印。
如果我们有一个包含换行符的字符串并且需要在同一行打印它,请使用 str.splitlines()
方法。
my_str = "fql\njiyik\n.com"
result = my_str.splitlines()
print(result) # 👉️ ['fql', 'jiyik', '.com']
for item in result:
# 👇️ fqljiyik.com
print(item, end='')
result = ''.join(result)
print(result) # 👉️ "fqljiyik.com"
str.splitlines
方法在换行符处拆分字符串并返回包含字符串中的行的列表。
str.splitlines
方法在不同的线边界上分割,例如\n
、\r
、\r\n
等
如果我们的字符串包含空白字符并且您需要在不带空格的情况下打印它,则可以使用 str.split()
方法。
my_str = "fql jiyik .com"
result = my_str.split()
print(result) # 👉️ ['fql', 'jiyik', '.com']
for item in result:
# 👇️ fqljiyik.com
print(item, end='')
result = ''.join(result)
print(result) # 👉️ "fqljiyik.com"
str.split()
方法使用定界符将字符串拆分为子字符串列表。
当没有分隔符传递给 str.split()
方法时,它会将输入字符串拆分为一个或多个空白字符。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。