[Solved] Operations on strings | Sololearn: Learn to code for FREE!


[Solved] Operations on strings

If I had a string that consists of multiple words seperated by spaces, would it be possible to operate on each word seperately; and is there a function for reversing a string?

12/29/2019 8:30:53 PM


7 Answers

New Answer


split the input into a list nums = input().split() this will create a list which can be traversed using while or for loop for e in nums: # e is each word in the list print(e[::-1]) lst[::-1] reverses a list


You can split the words in a sentence with split() function of string object in python. Save the result in a list then you can do whatever you want to them


Phantom split() function is build-in and you can call it for Strings ex:- "hey there".split(" ") It will return two strings("hey"," there")


Thank you so much for your help. ~ swim ~


Now it's clear. Thank you so much. Saboor Hakimi šŸ‡¦šŸ‡«


Just to add to what other people have posted (might be of interest to someone):- print("hello world =>", *"hello world"[::-1].split()[::-1]) => olleh dlrow print("hello world =>", *"hello world"[::-1].split()) => dlrow olleh print("hello world =>", *"hello world".split()[::-1]) => world hello


Does it need defining or is it built-in? Saboor Hakimi šŸ‡¦šŸ‡«