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

0

[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

Issa

7 Answers

New Answer

+4

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

+3

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

+2

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

+1

Thank you so much for your help. ~ swim ~

+1

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

+1

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

0

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