How to Split Text [Unstring ] in Python

Here is the best example that shows splitting a string in Python. I have taken the address string as an example to show how it works here. The split (unstring) function in Python is helpful for split strings.


How to split text using split function



String to use as input

Historically, the address is lengthy. It contains the door number, floor number, street number, and street name.

Rama Krishna 20/3 ABC street EFG Nagar US 234567

My task is I want to split it.

How to use the split function

Here, I used the strip function, which removes both leading and trailing spaces. Then, I used the split function, which splits the text.

Python program

user_address = input("Enter an address")

if user_address.strip(): #check that string is not empty (after removing leading
#and trailing spaces)

print("Your address is " + user_address)
split_address = user_address.split()
print(split_address)

Program output

Interactively, it asks the user to enter the address. After the code runs, the address splits into separate strings in a list.


Enter an address
Rama Krishna 20/3 ABC street EFG Nagar US 234567
Your address is Rama Krishna 20/3 ABC street EFG Nagar US 234567
['Rama', 'Krishna', '20/3', 'ABC', 'street', 'EFG', 'Nagar', 'US', '234567']


** Process exited - Return Code: 0 **

Press Enter to exit terminal

Post a Comment

Thanks for your message. We will get back you.

Previous Post Next Post