Ruan Bekker's Blog

From a Curious mind to Posts on Github

Splitting Characters With Python to Determine Name Surname and Email Address

I had a bunch of email addresses that was set in a specific format that I can strip characters from, to build up a Username, Name and Surname from the Email Address, that I could use to for dynamic reporting.

Using Split in Python

Here I will define the value of emailadress to a string, then using Python’s split() function to get the values that I want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> emailaddress = "ruan.bekker@domain.com"
>>> emailaddress.split("@", 1)
['ruan.bekker', 'domain.com']
>>> username = emailaddress.split("@", 1)[0]
>>> username
'ruan.bekker'
>>> username.split(".", 1)
['ruan', 'bekker']
>>> name = username.split(".", 1)[0].capitalize()
>>> surname = username.split(".", 1)[1].capitalize()
>>> name
'Ruan'
>>> surname
'Bekker'
>>> username
'ruan.bekker'
>>> emailaddress
'ruan.bekker@domain.com'

Print The Values in Question:

Now that we have define our keys, let’s print the values:

1
2
>>> print("Name: {0}, Surname: {1}, UserName: {2}, Email Address: {3}".format(name, surname, username, emailaddress))
Name: Ruan, Surname: Bekker, UserName: ruan.bekker, Email Address: ruan.bekker@domain.com

From here on you can build up for example an email function that you can pass the values to your function to get a specific job done.

Update: Capitalize from One String

Today, I had to capitalize the name and surname that was linked to one variable:

1
2
3
4
>>> user = 'james.bond'
>>> username = ' '.join(map(str, [x.capitalize() for x in user.split(".")]))
>>> print(username)
James Bond