【问题标题】:How to capitalize the first letter of a user-inputted string but keep the rest of the string's capitalization如何将用户输入的字符串的第一个字母大写,但保留字符串的其余部分大写
【发布时间】:2021-07-04 21:06:08
【问题描述】:

基本上正如标题所说,我希望用户输入的句子大写,但在此过程中不会丢失它们的大写。输入应该是两个用句点分隔的句子。我在这里的代码输出了句子,但没有加入或保留其余的大写。

def main():

 user_input = input("Enter the sentence you would like to be modified!: ").split(". ")
 capitalized_sentences = [user_input[0].upper() + user_input[1:] for sentence in user_input]
 recombined_sentences = ". ".join(capitalized_sentences)

【问题讨论】:

  • 你的意思是你只希望每个句子的第一个单词大写吗?你所拥有的应该这样做。向我们展示一些示例输入,以及您得到了什么以及您的期望。
  • 当我运行我所拥有的东西时,它会取消用户输入的所有其他内容。例如,如果有人在哪里输入“我最喜欢的角色是查理布朗。他是一项伟大的运动。”,它会将句子大写,但删除查理布朗的大写。我希望它保留所有其他大小写以及每个句子的第一个单词大写。
  • 所以你真的不想要capitalize()。您只想用它的.upper() 替换第一个字符。提示足够了吗?

标签: python string user-input capitalization


【解决方案1】:

只需将每个拆分的第一个字符编辑为大写:

# For this example, lets use this string. However, you would still use
# user_input = input("...") in your actual code
user_input = "for bar. egg spam."

# Turn the user_input into sentences.
# Note, this is assuming the user only is using one space.
# This gives us ["foo bar", "egg spam"]
sentences = user_input.split(". ")

# This is called a list comprehension. It's a way of writing
# a for-loop in Python. There's tons of documentation on it
# if you Google it.
#
# In this loop, the loop variable is "sentence". Please be mindful
# that it is a singular form of the word sentences.
#
# sentence[0].upper() will make the first letter in sentence uppercase
# sentence[1:] is the remaining letters, unmodified
#
# For the first iteration, this becomes:
# "f".upper() + "oo bar"
# "F" + "oo bar"
# "Foo bar"
capitalized_sentences = [sentence[0].upper() + sentence[1:] 
                         for sentence 
                         in sentences]

# At this point we have ["Foo bar", "Egg spam"]
# We need to join them together. Just use the same ". " we
# used to split them in the beginning!
#
# This gives us "Foo bar. Egg spam."
recombined_sentences = ". ".join(capitalized_sentences)

用您的user_input 位替换“句子”

请注意,如果用户输入了您不期望的格式的句子,则可能会出现“问题”。例如,如果用户输入了两个空格而不是一个空格怎么办?然后上面的代码将尝试将空格字符大写。你需要考虑到这一点。

【讨论】:

  • 我尝试通过自己的调整来使用这个设置,但它给了我一个类型错误,我不太清楚为什么。 “capitalized_sentences = [user_input[0].upper() + user_input[1:] for sentence in user_input] TypeError: can only concatenate str (not "list") to str
  • type(user_input) 给你什么?通过编辑你的问题告诉我你做了什么
  • 我在我的问题中更改了我的代码。我无法让它在不崩溃的情况下提取类型。
  • 问题是您没有正确调整我的代码。它应该是sentence[0].upper()sentence[1:]。您只需将第二行的最后一部分(即句子s)替换为user_input。强调单数与复数。 Sentences 表示句子列表,而 sentence 表示单个句子字符串。如果您需要更多说明,请告诉我。
  • 我改变了它并让它工作!非常感谢,我已经坚持了很长时间了。您是否有机会解释一下这段代码中发生了什么,以便我下次知道?
【解决方案2】:

很简单:可以对字符串的一部分使用String方法upper()。

这里有一个可以做到这一点的单行代码:

CapWord = "".join([c.upper() if i == 0 else c for i, c in enumerate([j for j in rawWord])])

只需将 CapWord 和 rawWord 替换为各自的值(您可以根据自己的需要将它们更改为句子/单词。

它的作用:

  • 使用字符串中的所有字符及其各自的枚举迭代数组(以避免重复的字母也被大写),然后检查 char (c) 是否有对应于要大写的索引的数字,并转换为一个字符串。

【讨论】:

  • 这仍然是一个糟糕的解决方案,因为当您知道您只会调整第一个字母时,您正在浪费地处理每个字母。
  • 绝对正确,如果 OP 要求更复杂但更有效的解决方案,该功能将被仔细优化,这很简单(r)。
猜你喜欢
  • 2020-12-10
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 1970-01-01
  • 1970-01-01
  • 2021-03-10
相关资源
最近更新 更多