0

My professor showed the class some code we can use to display the initials of a name, but she never explained how it works. The textbook doesn’t help much either. I am confused only on the second line. This isn’t for homework; I just want to know what makes it display the first letters in the string.

userName = input("Enter your name: ")
initials = '.'.join(name[0].upper() for name in userName.split())
print(initials)
2
  • The second line contains string indexing, a generator expression, and calls to str.join, str.upper, and str.split.Which of those things are you unfamiliar with? Commented Nov 7, 2015 at 0:26
  • This code is buggy; the final initial isn't followed by a period. Commented Nov 7, 2015 at 0:33

3 Answers 3

1

Say userName = "john paul jones" after the input is run.

Then userName.split() splits the name on whitespace ' ', returning an list which consists of the strings `"john", "paul", "jones" (in that order).

The for name in userName.split() part iterates over this -- hence name is successively bound to "john" then "paul" then "jones".

When name is bound to e.g. "paul" the expression name[0] returns the first character (e.g. 'p') which is then turned to the corresponding upper case (if it isn't already upper case). The net result is that the generator expression

(name[0].upper() for name in userName.split())

successively returns the strings 'J' then 'P' then 'J' (in our example).

Finally, join takes an iterable of strings and joins them together, using as a delimiter the string that join is called on -- in this case '.' -- think of this delimiter as the glue which holds the strings in the iterable together. When thus joined -- the final result is "J.P.J".

Your instructor probably should have tacked on a +'.' at the end of the expression to get a final period ("J.P.J.").

Sign up to request clarification or add additional context in comments.

1 Comment

Actually the final result is J.P.J in this case.
1
userName = input("Enter your name: ")

First, userName is a string, for example my name: kevin guan.

'.'.join(name[0].upper() for name in userName.split())

Now, name[0].upper() for name in userName.split() is a generator comprehension(it gives a generator):

userName.split()

gives a list like ['kevin', 'guan'].

name[0].upper() for name in ['Kevin', 'Guan']

You'll get ['K', 'G'], because name[0] is k in first for loop, then .upper() covert it to K and save it to a list(actually it's a generator). And at the second loop, we got G.

Then, '.'.join gives K.G. For more info, please check the document about str.join() and generator comprehension.

Comments

1

Let's take it apart:

initials = '.'.join(name[0].upper() for name in userName.split())

There is an inner loop here, for name in userName.split(). This is splitting the string in userName into chunks, or names. The documentation for split() says:

Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed).

So if the string contains spaces, it will split the string as you would expect. "my lastname" would be split into a list ["my", "lastname"]. This list becomes the list of names in the for loop. name would become the values "my" and "lastname" in this case.

Next, we have the cryptic function name[0].upper(). This is applied to each value of name from the for loop. The first part, name[0] takes the first character of the string in name. The second part, .upper() converts that first character (which is actually a one-characters string) into an uppercase character. This is described in the same documentation as above.

Continuing our example, name[0].upper() takes the two strings "my" and "lastname", takes the first letter from each one and converts it to uppercase: "M" and "L". The resulting list is ["M", "L"].

Finally, the '.'.join() expression takes the list inside of the join() and joins them together using the '.' character. In this case, the result is "M.L".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.