Python Strings: Medical Insurance Project

How do I split and remove the last name from this exercise?

Please post a link to this project in a reply. It will help if we can see the original data, the narrative and the instructions.

It looks like names is a list of strings that are in the form "FIRSTNAME LASTNAME"
Look up using .split for strings to split a string into a list of strings.

link to project:

1 Like

1 Like
a.strip('\n ').split(' ')[0].upper()

Above, pretend that a is the name string. After cleaning we split it on the space it contains, and capture only the first element, which will be a string, then we up-case it.


What is the name of the string? is it insurance_costs?

medical_records_clean consists of records with four strings. If the name is kept in tact (first and last) then that will be the first in the list. What I did was reduce the name be only including the first in my construction of that list.

If our record is called, x (for example), then unpacking it we get,

a, b, c, d = x

That is where I get the a from.

for x in medical_records:
  a, b, c, d = x
  y = []
  y.append(a.strip('\n ').split(' ')[0].upper())
  # ...
1 Like

The name of the string is “name”.
Your final code will look like this:

for i in range(len(names)):
  name = names[i]
  age = ages[i]
  bmi = bmis[i]
  cost = insurance_costs[i]
  print(name.strip('\n ').split(' ')[0].title() + " is " + age + " year old with a BMI of " + bmi + " and an insurance cost of " + cost +"\n")
1 Like