【问题标题】:I'm not getting any output in the correct code (2nd code)?我没有在正确的代码(第二个代码)中得到任何输出?
【发布时间】:2019-11-21 03:44:28
【问题描述】:

您好,提前感谢您的帮助。

这是与正确输出一起使用的代码。

first_name="ada"
last_name="lovelace"
full_name=f"{first_name} {last_name}"
message=(f"Hello, {full_name.title()}!")
print(message)

这是没有输出的类似代码...我不知道为什么?

first_name="ada"
last_name="lovelace"
full_name=f"{first_name} {last_name}"
print(full_name)

根据老师的回答,我知道这是正确的编码,但不知道为什么?

感谢您的帮助!

【问题讨论】:

  • 在 Python 3.6.5 上对我来说都可以正常工作。你如何执行它们?
  • 虽然两者都像 Selcuk 建议的那样工作,但由于缺少 .title() 功能,第二个没有大写。请参阅我对解决问题的可能方法的回答。

标签: python-3.x format-string title-case


【解决方案1】:

您忘记在字符串格式化请求中添加.title()

你可以这样做:

# to get both capitalized add .title() twice here at "full name".

first_name="ada"
last_name="lovelace"
full_name=f"{first_name.title()} {last_name.title()}"   
print(full_name)

或:


# to get both capitalized add .title() once here at "message".

first_name="ada"
last_name="lovelace"
full_name=f"{first_name} {last_name}"
message=(f"{full_name.title()}")
print(message)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-05
    • 2021-02-07
    • 2020-09-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 2016-12-01
    相关资源
    最近更新 更多