+ 1
How is String slice useful?
If possible explain by giving an example .
2 ответов
+ 1
It actually has many uses. For example: when asking for uset conformation, you can make it so that they can enter both "Y" and "yes"
i = input("You like Python? Y / N\n")
if i[0].lower() == 'y':
print("I thought so")
else:
print( ":(" )
Another example is grouping related variables. Say you have a big dictionary of names with cellphone numbers. You can choose to only display people starting with a given substring
namedict = {"Thomas": 1027,
"Abigail": 2080
"Gavin": 3466}
name = input("Name")
print(*[(n + '\n') for n in namedict if n.startswith(name)])
The examples given might not be optimised or functional, but I hope you get the message
+ 1
"(Let's get rid of the parentheses)"[1:-1]