如何更改Python中的大小写以使大写字母变为小写字母,反之亦然
专题7:问题4 编写函数changeCaseword,该函数更改单词中所有字母的大小写并返回新词 例子如何更改Python中的大小写以使大写字母变为小写字母,反之亦然,python,string,python-2.7,Python,String,Python 2.7,专题7:问题4 编写函数changeCaseword,该函数更改单词中所有字母的大小写并返回新词 例子 >>> changeCase('aPPle') "AppLE" >>> changeCase('BaNaNa') 'bAnAnA' 我是python的初学者,我的错误在哪里 def changeCase(word): return ''.join(c.upper() if c in 'aeiou' else c.lower() for c in w
>>> changeCase('aPPle')
"AppLE"
>>> changeCase('BaNaNa')
'bAnAnA'
我是python的初学者,我的错误在哪里
def changeCase(word):
return ''.join(c.upper() if c in 'aeiou' else c.lower() for c in word)
使用:
我对这个问题的解决方案,不需要像上面那样手动更改一个字母的大小写
def changeCase(word):
newword = "" # Create a blank string
for i in range(0, len(word)):
character = word[i] # For each letter in a word, make as individual viarable
if character.islower()== False: # Check if a letter in a string is already in upper case
character = character.lower() # Make a letter lower case
newword += character # Add a modified letter in a new string
else:
character = character.upper() # Make a letter upper case
newword += character # Add a modified letter in a new string
return newword # Return a new string
let=字母表
printlet.swapcase
o/p==字母表
SwapCase函数将返回字符串的副本,其中大写字符转换为小写,反之亦然
更多信息:请参阅文档如果“aeiou”中的c不是字母表中的所有小写字母。提示:检查islower方法谢谢您的代码片段,它可能提供一些有限的、即时的帮助。通过说明为什么这是一个很好的问题解决方案来正确解释它的长期价值,并将使它对未来有其他类似问题的读者更有用。请在您的回答中添加一些解释,包括您所做的假设。例如,我不知道ä如何变成Ä,反之亦然。更合理的做法是使用然后显式写入所有字母……这与2014年12月1日之前也说使用swapcase有什么不同?这段代码有很多问题:命名变量str是个坏主意。为什么在rangelens:中使用i而不是在s:中使用elem?在同一个循环中,您编写了三次append,只需使用一个变量保存新字符,然后在末尾追加它。要将列表转换为字符串,只需使用.joinsome\u list。返回时不应该有任何括号。如上所述,在哪里?答案可以而且确实会相对彼此移动。此外,for循环应该简化为for character in word:,并且==False既不规则又多余。这是与相同的解决方案,比它早6年。
def changeCase(word):
newword = "" # Create a blank string
for i in range(0, len(word)):
character = word[i] # For each letter in a word, make as individual viarable
if character.islower()== False: # Check if a letter in a string is already in upper case
character = character.lower() # Make a letter lower case
newword += character # Add a modified letter in a new string
else:
character = character.upper() # Make a letter upper case
newword += character # Add a modified letter in a new string
return newword # Return a new string
def swap_case(s):
input_list = list(s)
return "".join(i.lower() if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" else i.upper() for i in input_list)
if __name__ == '__main__':
s = input()
result = swap_case(s)
print(result)
txt = "aPpLe"
print (txt.swapcase())
#Here is another example to convert lower case to upper case and vice versa
while not bothering other characters:
#Defining function
def swap_case(s):
ls=[]
str=''
# converting each character from lower case to upper case and vice versa and appending it into list
for i in range(len(s)):
if ord(s[i]) >= 97:
ls.append(chr((ord(s[i]))-32)) #ord() and chr() are inbuild methods
elif ord(s[i]) >= 65 and ord(s[i]) <97:
ls.append(chr((ord(s[i]))+32))
# keeping other characters
else:
ls.append(chr(ord(s[i])))
# converting list into string
for i in ls:
str+=i
return(str)
if __name__ == '__main__':
s = input()
# calling function swap_case()
result = swap_case(s)
print(result)
input: www.SWAPCASE@program.2
Output: WWW.swapcase@PROGRAM.2