Split a String and Join

4/5 - (1 vote)

For a given a string, split the string on a ” ” (space) delimiter and join using a – hyphen

String split() method

The split() method splits a string into a list of strings.

Read here about Strings in Python, Operations & methods
You can specify the separator, the default separator is any whitespace.
Also, multiple spaces do not matter, they are treated as a single space.

String join() method

The join() method takes all items in an iterable and joins them into one string.

A string must be specified as the separator.

Solution

my_str = input('Enter text: ')
my_str = my_str.split()
my_str = '-'.join(my_str)
print(my_str)

//
Enter text: welcome to webpedia
welcome-to-webpedia

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net