2024-05-07 23:49:12 发布
网友
word = input("Enter a word: ") word_length = len(word) print("The length of", word,"is ", word_length)
如果word是'back',那么输出将是:
'back'
The length of back is 4
我希望输出为:
The length of 'back' is 4
您可以使用单词的repr来包含Python引号(例如,它们出现在交互式解释器中);这有一个额外的好处,即当引号已经在字符串中时,可以合理地工作:
repr
print("The length of", repr(word), "is", word_length)
最好的方法是使用^{}
print("The length of '{}' is {}".format(word, word_length))
使用C-like语句时,请注意这一点没有被强调(但还没有正式地弃用)
print("The length of '%s' is %s" % (word, word_length))
一种方法是使用'和^{} attribute将字符串相加
'
print("The length of '", word,"' is ", word_length, sep = '')
您可以使用单词的
repr
来包含Python引号(例如,它们出现在交互式解释器中);这有一个额外的好处,即当引号已经在字符串中时,可以合理地工作:好办法是:
最好的方法是使用^{}
坏方法:
使用C-like语句时,请注意这一点没有被强调(但还没有正式地弃用)
丑陋的方式:
一种方法是使用} attribute 将字符串相加
'
和^{相关问题 更多 >
编程相关推荐