How is String slice useful? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How is String slice useful?

If possible explain by giving an example .

1st Jun 2019, 1:45 PM
Da Talking Pigeon
Da Talking Pigeon - avatar
2 Answers
+ 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
1st Jun 2019, 2:05 PM
Trigger
Trigger - avatar
+ 1
"(Let's get rid of the parentheses)"[1:-1]
1st Jun 2019, 3:08 PM
Seb TheS
Seb TheS - avatar