Reputation: 5
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)
Upvotes: 0
Views: 64
Reputation: 13158
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 name
s. 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"
.
Upvotes: 1
Reputation: 51998
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."
).
Upvotes: 1
Reputation: 22282
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.
Upvotes: 1