sentence = str ( input ( "Enter a sentence:" ) )
sentence = sentence.split ( )
new = ""
for word in sentence:
wordi = ord ( word[ 0 ] )
cap = word[ 0 ]
a = chr ( (ord ( cap ) - 32) )
word1 = word.replace ( word[ 0 ] ,a )
if wordi <= 122 and wordi >= 97:
new = new + word1 + " "
else:
new = new + word + " "
print ( new )
我一直在写一个代码,它可以在不使用大写或大写函数的情况下将句子中的所有第一个字母大写。当单词中的字母与我想大写的字母不同时,我写的代码确实看起来没问题。
输入:
Hello world
输出:
Hello World
但是,如果单词中的字母也与我要大写的字母相同,则单词中的字母也会大写。
输入:
helloh worldw
输出:
HelloH WorldW
我尝试在替换中切换“a”变量,并在 if-else 语句中的变量 new 中添加 a 到 new。
sentence = str ( input ( "Enter a sentence:" ) )
sentence = sentence.split ( )
new = ""
for word in sentence:
wordi = ord ( word[ 0 ] )
cap = word[ 0 ]
a = chr ( (ord ( cap ) - 32) )
word1 = word.replace ( word[ 0 ] ,"" )
if wordi <= 122 and wordi >= 97:
new = new + a + word1 + " "
else:
new = new + word + " "
print ( new )
但是,代码原来是单词中重复的字母在打印时会被删除。
输入:
helloh
输出:
Hello
我如何才能使代码工作?
答案 0 :(得分:2)
您可以使用str.replace
方法的count
参数,只替换一次
word1 = word.replace(word[0], a, 1)
把所有的都放在一个方法里,方便使用,你就得到了
def capitalize(sentence):
if isinstance(sentence, str): # handle string, if wasn't splitted already
sentence = sentence.split()
new = ""
for word in sentence:
ow = ord(word[0])
if 97 <= ow <= 122:
new += word.replace(word[0], chr(ow - 32), 1) + " "
else:
new += word + " "
return new
print(capitalize(["Hello", "worldw"])) # Hello Worldw
print(capitalize("Hello worldw")) # Hello Worldw
答案 1 :(得分:2)
def capitalize(lower_case_word):
return ' '.join(x[:1].upper() + x[1:] for x in lower_case_word.split())
print(capitalize('hello I am nobody'))
要大写所有第一个字符,这是效率最高的一个(按行)。
def capitalize(lower_case_word):
lower_case_word = lower_case_word.split()
new_phrase = ""
for character in lower_case_word:
new_phrase += character[0].upper() + character[1:] + ' '
return new_phrase
print(capitalize('hello I am nobody'))
这个也可以,但是需要多行几行才能完成编码。个人而言,如果你是初学者,我推荐第二种方法,因为我也是初学者,它更容易理解。
答案 2 :(得分:1)
既然不能使用class PrivateAttachment(models.Model):
file = models.FileField(verbose_name="Object Upload",
storage=MinioBackend(bucket_name='my-test-bucket'),
upload_to=iso_date_prefix)
方法,那我们自己定义一个str.upper()
函数:
upper()
您只想用大写字母替换第一个字符。不幸的是,字符串是不可变的,因此您不能简单地执行 def upper(char):
# If more than one characters are given, capitalize all of them
if len(char) > 1:
return ''.join(upper(c) for c in char)
# if given character is between lowercase a and z, shift it to uppercase
if "a" <= char <= "z":
return chr(ord(char) - 32)
else: # if not, return it unchanged
return char
但是,您可以通过从其余字符中切出第一个字符来重建字符串,如下所示:word[0] = word[0].upper()
。
或者,使用我们的 word = word[0].upper() + word[1:]
函数:upper()
。
用您的代码实现这一点:
word = upper(word[0]) + word[1:]
当然,for 循环可以替换为列表推导式,
sentence = str ( input ( "Enter a sentence:" ) )
sentence = sentence.split ( )
newsentence = []
for word in sentence:
newword = upper(word[0]) + word[1:]
newsentence.append(newword)
print(' '.join(newsentence))
使用您的输入 sentence = str ( input ( "Enter a sentence:" ) )
sentence = sentence.split ( )
newsentence = [upper(word[0]) + word[1:] for word in sentence]
print(' '.join(newsentence))
# Or you could pass the generator expression to the `str.join()` call
# To do this, replace the previous two lines with:
# print(' '.join(word[0].upper() + word[1:] for word in sentence))
运行此程序会得到您期望的输出:
helloh worldw