Python Forum

Full Version: Captalizing the letter from the first two names
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to write a program that asks the user for his/her name
the program should take the name of the user from 1 line input and gives an output with Welcome+the first letter capitalized of both names

for example:
input               output
riko johns         Welcome "RJ"

I have tried this but the problem is i dont know how to identify the second name in order to capitalize its first letter
x= raw_input("Write your full name: ")
y= x[0:1]
z= y.upper()
print "Welcome",'"'+z+'"'
Strings have split method that returns list of "words".

Output:
>>> x = "rico johns" >>> x.split() ['rico', 'johns']
(Mar-10-2017, 08:07 PM)zivoni Wrote: [ -> ]Strings have split method that returns list of "words".

Output:
>>> x = "rico johns" >>> x.split() ['rico', 'johns']

how can i use this to identify the inputs into value
for example
x="name 1"
y="name 2"
You can access list elements by indexing with [].

x = "rico johns"
words = x.split()
first_word = words[0]
second_word = words[1]
And you dont need to use x[0:1] to get first char of string - just x[0] is enough.
By referring to the index value of the list (remember to start with '0')

>>> x = 'rico johns'
>>> names = x.split()
>>> names[0]
'rico'
>>> names[1]
'johns'
>>>
Quote:first letter capitalized of both names

Are you aware there is already a string method that does this?
>>> 'metul burr'.title()
'Metul Burr'
Thank you guys that really helped me