在 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!'

Python 中打印变量之间没有空格

第一个示例使用格式化字符串文字来打印值,它们之间没有空格。

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)

Python 中 TypeError can only concatenate str to str

要解决这个问题,我们必须转换其中一个值。

print('abc' + str(123)) # 👉️ 'abc123'

使用格式化字符串文字时,我们不必确保值的类型兼容,因为 f-strings 会自动为我们将值转换为字符串。

在 Python 中不使用换行符或空格打印

使用 print() 函数的 endsep 参数在没有换行符或空格的情况下打印。

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() 方法时,它会将输入字符串拆分为一个或多个空白字符。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迹忆客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值