We will slice the email address and get the username and domain using three different methods:
1) The conventional method of slicing
2) Using split function
3) Regex(Regular Library) Library
So, let's start looking at each method in detail with the code.
1) Slicing
First of all we will ask for the email-address. Then we will write two expressions to slice the email address each for username and domain.
Finally we will print the username and domain.
2) Using Split function
Here we will input the email-address. Then we will split the email address from '@' using just one statement. i.e. email.split('@'). It will split the string from '@' and return list. Then we will print the list
3)Using Regex
Here, we will first import the re module. Then we will ask for the user input (Email address to be sliced).
Then we will write a regular expression which will split our string from '@'. We can also split from more than one element by just adding more elements with a space with @. The print(result) will print a sliced list because the regex expression will return a list which is been splitted by '@'
For example if I want my email address to split from '.' and '#' too, then I will write the expression as follows,
Then we will print the list which was splitted, here specifically we will print using the f string. Although we can use any method to print. Below is the complete code for the Email Slicer using regex library
No comments:
Post a Comment