Given a string as input to the Python program the python script should get all the substance present in a string and then print it.
let's use the built in string function to split() the string into substring.
Write A Python Script To Get All The Substrings Of A Given String
Let's take a variable named as text and ask the user to enter a string, Once we get the string as input convert it into substring using its split function call. Use space as an input argument to the function call as a separator as should below.
text = input("Enter a string : ")
substrings = text.split(" ")
Use the print function call to print a user friendly message, then using the for loop iterate all the items present in the substring, by Converting the substring into string and then print it.
print("Substring of a Given String is : ")
for item in set(substrings):
print(item)
Complete Python program
==============================
text = input("Enter a string : ")
substrings = text.split(" ")
print("Substring of a Given String is : ")
for item in set(substrings):
print(item)
Output
================
Enter a string : hi, how are you
Substring of a Given String is :
hi,
how
are
you
Output
================
Enter a string : Write A Python Script To Get All The Substrings Of A Given String
Substring of a Given String is :
Python
Get
All
Of
Write
Given
Script
A
Substrings
String
To
The
Conclusion
=======================
Execute the above Python program by providing a random string as a input to the Python program,
Comment it down below if you have any query is regarding the above python program
0 Comments