[英]String Concatenation and Formatting | Python
word1 = input("Random Name (ex. Ryan):")
word2 = input("Your name (ex. why do you need an example of your name?):")
print("To:", word1, "@gmail.com")
print("From:", word2, "@outlook.com")
现在,您几乎需要忽略除单词1和word2之外的所有内容。
我只是想知道为什么输出是
To:name @gmail.com
而不是 To:name@gmail.com
在此先感谢大家!
因为在python中, print
自动在print语句的参数之间添加空格,即用逗号分隔的内容。 如果要删除它,可以将sep
关键字设置为除空格以外的其他分隔符。 将其设置为空字符串不包含分隔符。
>>> print('A','string','with','spaces.')
A string with spaces.
>>> print('A','string','with','no','spaces.', sep = '')
Astringwithnospaces.
你可以做
print("To:" + word1 + "@gmail.com")
要么
print("To:%s@gmail.com" % word1)
要么
print("To:{}@gmail.com".format(word1))
或在评论中建议:
print("To:{0:s}@gmail.com".format(word1))
以所需方式打印值
进行print(some_val, some_val2, some_val3, ...)
将在这些值之间添加一个空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.