Featured Post

How to Create a Symmetric Array in Python: A Fun Logic Exercise

Image
 Here's a Python program that says to write a Symmetric array transformation. A top interview question. Symmetric Array Transformation Problem: Write a Python function that transforms a given array into a symmetric array by mirroring it around its center. For example: Input: [1, 2, 3] Output: [1, 2, 3, 2, 1] Hints: Use slicing for the reverse part. Concatenate the original array with its mirrored part. Example def symmetric_array(arr):     """     Transforms the input array into a symmetric array by mirroring it around its center.     Parameters:     arr (list): The input array.     Returns:     list: The symmetric array.     """     # Mirror the array by concatenating the original with its reverse (excluding the last element to avoid duplication)     return arr + arr[-2::-1] # Example usage input_array = [1, 2, 3] symmetric_result = symmetric_array(input_array) print("Input Array:", input_arr...

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

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

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)