Featured Post

Python Regex: The 5 Exclusive Examples

 Regular expressions (regex) are powerful tools for pattern matching and text manipulation in Python. Here are five Python regex examples with explanations: 01 Matching a Simple Pattern import re text = "Hello, World!" pattern = r"Hello" result = re.search(pattern, text) if result:     print("Pattern found:", result.group()) Output: Output: Pattern found: Hello This example searches for the pattern "Hello" in the text and prints it when found. 02 Matching Multiple Patterns import re text = "The quick brown fox jumps over the lazy dog." patterns = [r"fox", r"dog"] for pattern in patterns:     if re.search(pattern, text):         print(f"Pattern '{pattern}' found.") Output: Pattern 'fox' found. Pattern 'dog' found. It searches for both "fox" and "dog" patterns in the text and prints when they are found. 03 Matching Any Digit   import re text = "The price of the

Python Split String: Techniques and Best Practices

Here is an example for splitting a string in Python. The functions you need are strip and split to split string, or text. 

python split string

Here's the string taken for split

My task is I want to split address string. 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

How to Split a string

The python split string works in two steps. The first step is to use strip function, which removes both leading and trailing spaces. Next, use the the split function, which splits the input string, or text into a list.

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()


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


Popular posts from this blog

Explained Ideal Structure of Python Class

6 Python file Methods Real Usage

How to Decode TLV Quickly