How is String slice useful? | Sololearn: Learn to code for FREE!


How is String slice useful?

If possible explain by giving an example .

6/1/2019 1:45:58 PM

Da Talking Pigeon

2 Answers

New Answer


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


"(Let's get rid of the parentheses)"[1:-1]