Featured Post

Python Set Operations Explained: From Theory to Real-Time Applications

Image
A  set  in Python is an unordered collection of unique elements. It is useful when storing distinct values and performing operations like union, intersection, or difference. Real-Time Example: Removing Duplicate Customer Emails in a Marketing Campaign Imagine you are working on an email marketing campaign for your company. You have a list of customer emails, but some are duplicated. Using a set , you can remove duplicates efficiently before sending emails. Code Example: # List of customer emails (some duplicates) customer_emails = [ "alice@example.com" , "bob@example.com" , "charlie@example.com" , "alice@example.com" , "david@example.com" , "bob@example.com" ] # Convert list to a set to remove duplicates unique_emails = set (customer_emails) # Convert back to a list (if needed) unique_email_list = list (unique_emails) # Print the unique emails print ( "Unique customer emails:" , unique_email_list) Ou...

The Easy Way to Split String Python Partition Method

Here's a way without the Split function you can split (or extract) a substring. In Python the method is Partition. You'll find here how to use this method with an example. 


Split string using the Partition

How to Split the string using Partition method

 

Returns Left side part

Example-1

my_string='ABCDEFGH||10||123456.25|'

my_partition=my_string.partition('|')[0]

print(my_partition)


Output

|10||123456.25|


** Process exited - Return Code: 0 **
Press Enter to exit terminal

Example-2

Returns from the separator to last of the string.

my_string='ABCDEFGH||10||123456.25|'
my_partition=my_string.partition('|')[-1]
print(my_partition)

Output

|10||123456.25|


** Process exited - Return Code: 0 **
Press Enter to exit terminal


The use of Rpartition to split a string in Python


Example-1

Returns except right side last separator.

my_string='ABCDEFGH||10||123456.25|'
my_partition=my_string.rpartition('|')[0]
print(my_partition)

Output

ABCDEFGH||10||123456.25


** Process exited - Return Code: 0 **
Press Enter to exit terminal

Example-2

Returns no string

my_string='ABCDEFGH||10||123456.25|'
my_partition=my_string.rpartition('|')[-1]
print(my_partition)


Output




** Process exited - Return Code: 0 **
Press Enter to exit terminal


Top Udemy courses

Comments

Popular posts from this blog

SQL Query: 3 Methods for Calculating Cumulative SUM

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

5 SQL Queries That Popularly Used in Data Analysis