Write a Python program to convert a given string into a list of words
The program is written in Python and demonstrates the usage of string manipulation methods in Python.
- str1 = "Tutor Joes Computer Education" creates a string variable str1 with the value "Tutor Joes Computer Education".
- print(str1.split(" ")) splits the string str1 into a list of substrings using the whitespace character " " as the separator. The resulting list is ['Tutor', 'Joes', 'Computer', 'Education'] and is printed to the console.
- str2 = "Tutor Joes,Computer Education" creates another string variable str2 with the value "Tutor Joes,Computer Education".
- print(str2.split(",")) splits the string str2 into a list of substrings using the comma "," as the separator. The resulting list is ['Tutor Joes', 'Computer Education'] and is printed to the console.
- str1 = "Tutor-Joes-Computer-Education" reassigns the value of str1 to be "Tutor-Joes-Computer-Education".
- print(str1.split("-")) splits the string str1 into a list of substrings using the dash "-" as the separator. The resulting list is ['Tutor', 'Joes', 'Computer', 'Education'] and is printed to the console.
- str1 = "2014-06-8" reassigns the value of str1 to be "2014-06-8".
- print(str1.partition("-")) splits the string str1 into a tuple of three substrings using the first occurrence of the dash "-" as the separator. The resulting tuple is ('2014', '-', '06-8') and is printed to the console
Source Code
str1 = "Tutor Joes Computer Education"
print(str1.split(" "))
str2 = "Tutor Joes,Computer Education"
print(str2.split(","))
str1 = "Tutor-Joes-Computer-Education"
print(str1.split("-"))
str1 = "2014-06-8"
print(str1.partition("-"))
Output
['Tutor', 'Joes', 'Computer', 'Education']
['Tutor Joes', 'Computer Education']
['Tutor', 'Joes', 'Computer', 'Education']
('2014', '-', '06-8')