• Log in or sign up for an account here: https://www.sythe.org/login

An initials problem (python)

Strings are like arrays of characters in most languages and you can access characters like an array.

For example the_initials[0] represents the first name, how do I make a program recognize what the first letter in the_initials[0] is?

To get the first character do the_initials[0][0]


EXAMPLE:
>>> hi = ("hello","test")
>>> print hi
('hello', 'test')
>>> print hi[1]
test
>>> print hi[1][0]
t
 
Strings are like arrays of characters in most languages and you can access characters like an array.



To get the first character do the_initials[0][0]


EXAMPLE:
>>> hi = ("hello","test")
>>> print hi
('hello', 'test')
>>> print hi[1]
test
>>> print hi[1][0]
t

I've been working on the problem all day and have made quite a bit of progress. I updated my first post with a new question aha.
 
How would I make it so that if the last name is entered in lowercase letters, the first letter of the last name will always be displayed with a capital? For example right now if I enter peter as the last name it will be displayed as peter.. I need it to be displayed as Peter. And another thing, how do I make it so that no matter how sloppy a name is entered it will be made neater? For example if the last name is entered peTEr, I need it to display Peter.

Theres a built in method called Title that will capitalize the last name properly.

just do the_initials[2].title() or whatever your last name vairiable is called.

http://docs.python.org/library/stdtypes.html#str.title


Just so you know there are much simpler ways to make the first initial. you could do something like

second_initial = the_initials[1][0].upper() + "."
 
Thanks! How would I have my initial() function return my new single string back to the main() function for printing? Also before you told me about second_initial = the_initials[1][0].upper() + "." I made a few changes, will that do to? Anything else you see wrong with my code or that I could make simpler?
 
Change
print "Nice to meet you:", first_initial, second_initial, the_initials[2].title()
to
return first_initial + " " + second_initial + the_initials[2].title()

and in your main function:

initials(full_name)
to
print "Nice to meet you:", initials(full_name)

and yes
first_initial = the_initials[0][0].upper() + "."
second_initial = the_initials[1][0].upper() + "."
should do the same thing as those 26 if statements.
 
Does this look perfect?

Code:
def main():
    full_name = raw_input("Please enter your full name (first, middle and last name with a space in between each name): ")
    print "Nice to meet you:", initials(full_name)


def initials(full_name):
    the_initials = full_name.split(" ")

    first_initial = the_initials[0][0].upper() + "."
    second_initial = the_initials[1][0].upper() + "."
       

    return first_initial + " " + second_initial + " " + the_initials[2].title()


main()

Didn't add the notes yet.
 
Back
Top