Knowee
Questions
Features
Study Tools

Write a Python program to split a string to wordsstring = "Hello World"print(string.split())string = "Hello World"print(string.split("o"))string = "Hello World"print(string.split("l"))string = "Hello World"print(string.split("W"))

Question

Write a Python program to split a string to wordsstring = "Hello World"print(string.split())string = "Hello World"print(string.split("o"))string = "Hello World"print(string.split("l"))string = "Hello World"print(string.split("W"))

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the explanation of the Python program you provided:

  1. First, you define a string variable with the value "Hello World".
string = "Hello World"
  1. Then, you use the split() method to split the string into a list of words. By default, split() divides the string at spaces.
print(string.split())

This will output: ['Hello', 'World']

  1. Next, you split the string at each occurrence of the letter "o".
print(string.split("o"))

This will output: ['Hell', ' W', 'rld']

  1. Then, you split the string at each occurrence of the letter "l".
print(string.split("l"))

This will output: ['He', '', 'o Wor', 'd']

  1. Finally, you split the string at each occurrence of the letter "W".
print(string.split("W"))

This will output: ['Hello ', 'orld']

In each case, the split() method is dividing the string at the specified character (or space by default) and returning a list of the resulting substrings.

This problem has been solved

Similar Questions

Write a Python program to split a string to words

Reverse words in a given String in Pythonstring = "Hello World"print(string[::-1])string = "Hello World"print(" ".join(string.split()[::-1]))string = "Hello World"print(" ".join(reversed(string)))string = "Hello World"print(" ".join(string.split()[::1]))

10.What will be the output of the following Python code?string = "my name is x"for i in string.split():    print (i, end=", ")

What will be the output of the following code? str1="an apple a day keeps doctor away"lis=str1.split(" ")for word in lis: if word.startswith("d") : lis.remove(word)print(lis)

What is the output of following?print(“abcabcab”.split(‘c’))

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.