gpt4 book ai didi

python - Pyschool 测验--字符串连接

转载 作者:行者123 更新时间:2023-11-28 22:03:56 24 4
gpt4 key购买 nike

这是测验问题:

Adding two strings or making multiple copies of the same string.

Examples:

greetings = "Hello World"

len(greetings) # get the length of string

 11

greetings[0] # get the 1st character

 'H'

print underline("Good Day")

 Good Day
________

# Write a function, given a string of characters, return the string together with '_'s of the same length.

我的第一次尝试是:

def underline(title): 
print title
print len(title) * '_'

...它在视觉上有些通过,但也返回“无”值。 (知道为什么会这样吗?)所以我尝试了:

def underline(title): 
print title, \nlen(title) * '_'

...并得到“行继续字符后的意外字符”错误。在谷歌对这个错误没有帮助后转向这里。

最佳答案

好吧,您不想打印那些字符串,您想要返回它们。

因此创建一个将它们组合在一起的字符串(由换行符分隔)并返回它。

def underline(title):
return title + '\n' + len(title) * '_'

关于python - Pyschool 测验--字符串连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7976873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com