怎么寫才能在比較時候不區分大小寫?Lion和liON不區分大小寫對比,并打印出已被占用。
current_users = ['Tifa', 'Ada', 'Lightning', 'Lion', 'Natasha']
new_users = ['Tifa', 'Ada', 'Lucifer', 'liON', 'Michael']
#下面是我寫的代碼,該如何在用戶名為liON時打印出'Username is uese'?
for new_user in new_users:
? ? print(new_user)
? ? if new_user in current_users:
? ? ? ? print('Username is uesd')
? ? else:
? ? ? ? print('Username is not used')
2017-12-12
全部轉為大寫或者全部轉為小寫再對比
2017-12-12
current_users = ['Tifa', 'Ada', 'Lightning', 'Lion', 'Natasha']
new_users = ['Tifa', 'Ada', 'Lucifer', 'liON', 'Michael']
for new_user in new_users:
? ?print(new_user)
? ?if new_user.lower().title() in current_users:
? ? ? ?print('Username is uesd')
? ?else:
? ? ? ?print('Username is not used')
2017-12-12
current_users = ['Tifa', 'Ada', 'Lightning', 'Lion', 'Natasha']
new_users = ['Tifa', 'Ada', 'Lucifer', 'liON', 'Michael']
for new_user in new_users:
? ? print(new_user)
? ? if new_user.lower() in [current_user.lower() for current_user in current_users]:
? ? ? ? print('Username is uesd')
? ? else:
? ? ? ? print('Username is not used')
2017-12-12
在比較前,先把字符串轉換成全部大寫或者小寫,再進行比較