Python Forum
Captalizing the letter from the first two names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Captalizing the letter from the first two names
#1
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+'"'
Reply
#2
Strings have split method that returns list of "words".

Output:
>>> x = "rico johns" >>> x.split() ['rico', 'johns']
Reply
#3
(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"
Reply
#4
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.
Reply
#5
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'
>>>
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
Quote:first letter capitalized of both names

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Capitalize first letter of names in nested loops student_of_python 9 4,829 Oct-27-2019, 07:51 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020