如果单词以大写字母开头,则交换字符串中的大小写

问题描述 投票:0回答:1

我需要输入一个输入句子,例如:Hello world,星期一。并输出:hELLO世界现在是星期一。如果单词以大写字母开头,我基本上需要交换大小写。我知道如何交换大小写,但是.isupper()始终返回false。

x = str(input("Enter your sentence please. "))

y = x.split(" ")

print(y)

######THIS OUTPUTS FALSE NO MATTER WHAT
for i in range(0,len(y)):
  if y[i].isupper() == True:
    print("True")
  else:
    print("False")  
python python-3.x uppercase lowercase
1个回答
0
投票

您需要:

a = "Hello world its Monday"

x = [i.swapcase() if i[0].isupper() else i for i in a.split()]

print(" ".join(x))

输出:

hELLO world its mONDAY
© www.soinside.com 2019 - 2024. All rights reserved.