.upper不能在python中工作

时间:2017-10-31 01:23:55

标签: python uppercase

我目前有这段代码

num_lines = int(input())
lines = []
tempy = ''
ctr = 1
abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
}
for i in range(0, num_lines):
  tempy = input()
  lines.append([])
  lines[i].append(tempy)


for o in range(0, num_lines):
  for p  in range(0, len(lines[o])):
    for u in range(0, len(lines[o][p])):
      if lines[o][p][u] in abc:
        lines = str(lines)
        if ctr % 2 == 0:
          print(lines[o][p][u])
          lines[o][p][u].upper()
        else:
          print(lines[o][p][u])
          lines[o][p][u].lower()
        ctr += 1

print(lines)

但.upper行似乎没有效果,有人可以告诉我为什么?

提前谢谢,如果有答案,请告诉我,我没有标记为重复原因,而是搜索了一个小时

1 个答案:

答案 0 :(得分:1)

.upper().lower()函数不会修改原始lines[o][p][u].upper()。每the documentation

lines[o][p][u].lower()

  

<强> str.upper()

     

返回字符串的副本,并将所有已设置的字符转换为大写。

lines[o][p][u]

  

<强> str.lower()

     

返回字符串的副本,并将所有已设置的字符转换为小写。

因此,如果您想分别使用大写和小写字符,则需要打印num_lines = int(input("How many words do you want to enter?: ")) originals = [] alternating = [] for i in range(num_lines): line = input("{}. Enter a word: ".format(i + 1)) originals.append(line) alternating.append("".join([x.lower() if j % 2 == 0 else x.upper() for j, x in enumerate(line)])) print("Originals:\t", originals) print("Alternating:\t", alternating) How many words do you want to enter?: 3 1. Enter a word: Spam 2. Enter a word: ham 3. Enter a word: EGGS Originals: ['Spam', 'ham', 'EGGS'] Alternating: ['sPaM', 'hAm', 'eGgS'] 而不是createNewAdminAccount。但是,如果我正确理解了您的目标,那么从您的代码示例中,您可能会尝试从字符串输入中替换大写和小写字符。您可以使用list comprehension更轻松地执行此操作,具体如下:

admin.auth().getUserByEmail()

使用以下示例输出:

auth/email-already-exists