Python|转换字符串列表中元素的大小写
给定一个字符串列表,编写一个Python程序将所有字符串从小写/大写转换为大写/小写。
Input : ['GeEk', 'FOR', 'gEEKS']
Output: ['geeks', 'for', 'geeks']
Input : ['fun', 'Foo', 'BaR']
Output: ['FUN', 'FOO', 'BAR']
方法 #1:使用map
函数将大写字母转换为小写字母
# Python code to convert all string
# from uppercase to lowercase.
# Using map function
out = map(lambda x:x.lower(), ['GeEk', 'FOR', 'gEEKS'])
# Converting it into list
output = list(out)
# printing output
print(output)
输出:
['geek', 'for', 'geeks']
方法 #2:使用列表推导将小写字母转换为大写字母
# Python code to convert all string
# from uppercase to lowercase.
# Initialisation
input = ['fun', 'Foo', 'BaR']
# Converting
lst = [x.upper() for x in input]
# printing output
print(lst)
输出:
['FUN', 'FOO', 'BAR']