0

I'm currently struggling to separate a string into two while using a white space as the base of the split.

I'm writing a program where a person is supposed to input their first and last name as a string. Then the program should take the first three letters out of the first name and first three letters out of the last name. Put these together and append them to a domain name creating an email. Such as Firstname Lastname becomes firlas@example.com. I know I could easily solve the problem by just using two inputs, but I want it to be only one input.

def create_email_address():
name = input("Please input your first and last name: ")
domain = "@aperturescience.com"


email_address = str.lower(name[:3]) + str.lower(name[10:13]) + domain
print(str(email_address))

This is what I have so far, and while I have experimented using list splits and indexes I can't get it to work properly. I can get the index to work when I use my own name as an example name. But if I input something that doesn't have the same amount of characters in it it doesn't perform as I want it to.

What I would like is to separate the first name and last name using the white space as the indicator where the split is supposed to be. That way it won't matter how long or short the names are, I can always index after the first three letters in each name.

Is this possible? and if so, any tips?

Thanks!

  • 1
    Does this answer your question? [How can I split and parse a string in Python?](https://stackoverflow.com/questions/5749195/how-can-i-split-and-parse-a-string-in-python) – fabianegli Feb 20 '20 at 22:03
  • 1
    @LukeRhinehart_ I've answered your questions under my answer – Demian Wolf Feb 20 '20 at 22:44

6 Answers6

3
def create_email_address():

  name = input("Please input your first and last name: ")
  domain = "@aperturescience.com"

  names=name.split()
  email_address = str.lower(names[0][:3]) + str.lower(names[1][:3]) + domain
  print(str(email_address))

The above code has been slightly modified to use split - which will create a list from your input string assuming that it is delimited by whitespaces.

Mark
  • 4,249
  • 1
  • 18
  • 27
  • 1
    This solved it for me. Thank you so much! If you don't mind me asking. How come name.split() + instead using [0][:3] and [1][:3] to work? What's the logic behind this, just so that I understand why. – LukeRhinehart_ Feb 20 '20 at 22:15
  • 2
    @LukeRhinehart_, firstly, `names` list was created when the `name` string was splitted. Secondly, you want to take the first or the second element of this list - you need to get it by index: [0] is for the first one, [1] is for the second one **(note: indexes in Python lists, tuples, strings always start from 0)**. Thirdly, when you've got the first or second element you use the slices to get needed part ([:3] is a slice) – Demian Wolf Feb 20 '20 at 22:24
  • 1
    Ah, I see. That index always starts from 0 was something I was aware of. But not that I'm able to specify the first or second element like so. Thank you for your explanation. Much appreciated! – LukeRhinehart_ Feb 20 '20 at 22:26
  • 1
    @LukeRhinehart_ I enjoyed helping you :) – Demian Wolf Feb 20 '20 at 22:47
2

Use str.split() method to get the first and last name. Note: if you don't specify attributes, it will split not only by space, but by all the space characters (e.g. "\n" - new line)

Here are the parts of your code which should be corrected:

1) It's better to use name[:3].lower() instead str.lower(name[:3]), though the code works fine in both cases. In Python, we almost always call method of a class from an already created instance (str_instance.lower() instead of str.lower(str_instance)

2) You shouldn't use print(str(...)), because print will convert the result to string even if you don't specify it explicitly. Moreover, in this case it is already a string.

So, here is the full fixed code:

first, last = input("Please input your first and last name: ").split()
domain = "@aperturescience.com"


email_address = first[:3].lower() + last[:3].lower() + domain
print(email_address)
Demian Wolf
  • 1,698
  • 2
  • 14
  • 34
  • Thank you Demian! I'd like to adress your points below. 1 I understand. I was currently experimenting with different lengths names and this code is just the final experiment before I decided to go here and ask what I was missing. But if I understand you correctly using [number:13] will limit the name to 13 characters? 2. I guess that's for ease of reading and being more pythonic? 3. I see, I didn't know it would automatically convert it. I used str() just to make sure that I actually was printing a string. Thank you for your help! – LukeRhinehart_ Feb 20 '20 at 22:24
  • @LukeRhinehart_, please see my answer again, I've fixed it – Demian Wolf Feb 20 '20 at 22:29
  • 1
    @LukeRhinehart_, I was very glad to halp you :) – Demian Wolf Feb 20 '20 at 22:31
  • 1
    @LukeRhinehart_, considering your first question - `[index:13]` (not a `number`, it is called **index**) will **not** limit the name to 13 characters - however it will only take the part of the string from a character with `index` index (not included) to the character with `13` index (included), that we don't need if we're trying to get the 3 **last** characters of the name. I've written that because I've misinterpreted your question - I've read you need to get the 3 **last** (not first) characters of the last name. You need to look at my answer again - I've updated it. – Demian Wolf Feb 20 '20 at 22:38
  • 1
    @LukeRhinehart_ And considering your second question - yes, it is really a more pythonic way and it is always used. – Demian Wolf Feb 20 '20 at 22:43
1

You can use str.split() like so:

# get user input:
full_name = input('Enter your full name: ')

# split user input on white space, this will return a list:
first_name = full_name.split()[0]
last_name = full_name.split()[1]

# get first three characters in string:
first_name_first_three = first_name[0:3]
last_name_first_three = last_name[0:3]

print(first_name_first_three)
print(last_name_first_three)
Daniel
  • 3,228
  • 1
  • 7
  • 23
  • IMHO, it's better to call full_name.split() only once and use unpacking then: `first_name, last_name = full_name.split()` – Demian Wolf Feb 20 '20 at 22:45
1

One of the best ways to do that would be to use regular expressions. Please refer to the examples below.

import re

pattern = re.compile('\W+')

name = "Jerry Maguire"

split_names = pattern.split(name)

split_names
Out[24]: ['Jerry', 'Maguire']

name = "FrodoBaggins"

split_names = pattern.split(name)

split_names
Out[27]: ['FrodoBaggins']

This would handle scenarios where there are no spaces or if name needs to be split on multiple lines.

Sagi
  • 322
  • 2
  • 9
1

While we're all giving solutions, I'll just give you the bad answer. A one liner:

>>> print("".join(["".join([i[:3].lower() for i in input("Enter your full name: ").split(" ")]), "@example.com"]))
Enter your full name: Albert Finksson
albfin@example.com

This one is almost unreadable though, for your sake and others in the future use the other solutions.

Hampus Larsson
  • 3,050
  • 2
  • 14
  • 20
1

I see that the use case where name contains more than two word (e.g. middle name) hasn't been addressed. So here's my attempt to answer,

def create_email():
   name = input("Enter Full Name : ")
   domain = "example.com"
   email = name[:3].lower() + name.split()[-1][:3].lower() + "@" + domain
   print(email)

Couple of pointers on the code above:

  1. you don't really need to split to get the first three letters of first name as it's just the first three letters of full name
  2. name.split()[-1] will get the last name from full name and [:3] will get the first three letters of last name
NFR
  • 316
  • 2
  • 11