Making a change to the first letter of the first string in a list


#1



I completed the tutorial, but I wanted to change the code to work for sentences, not just single words.

https://www.codecademy.com/courses/python-beginner-2W5v7/1/5?curriculum_id=4f89dab3d788890003000096


Code not changing the first letter of the sentence to uppercase.


I expected that the first character of the first string in word_list would become uppercase.


original = raw_input('Enter a word:')
lowercase = original.lower()
word_list = lowercase.split()

for word in word_list:
    if len(word) > 0 and word.isalpha and word[0] not in 'aeiou':
        while word[0]  not in 'aeiou':
            first = word[0]
            word = word[1:len(word)] + first
        if word == word_list[0]:
            print word_list[0][0].upper() + word[1:] + 'ay',
        else:
            print word + 'ay',


This gives:

Enter a word: Draco is a smart boy
acodray isway away artsmay oybayNone


#2

If you remove this line it will because this line overrides the uppercase D :slight_smile:


#3

The thing is I don't want to capitalize the D. I want the new 1st letter, in this case 'a', to be capitalized.


#4

Basically this part isn't doing what I thought it should be doing. Is it because the word variable has already changed from its original form in word_list? Since now it is 'acodray', but in the word_list it is still 'draco', and thus the comparison will never be True?
If so, what is a way to do it then?


#5

What you are trying to do cannot be done because python strings are immutable, which is just a long word for saying that once they've been created, you can't change them. Attempting to do so triggers an error. :slight_smile:

You can only slice strings,word_list[0] would return the first letter of user's input but you cannot modify them unless you would be handling the operations in separate variables.


#6

Hi @djdato ,

The capitalize method may help you. Here's an example of how it works ...

w = "HeLLo"
w = w.capitalize()
print w

Output ...

Hello

Notice that it returns a new string in which the first, and only the first, character of the string is uppercase.

You can modify your program so that after it uses split to make word_list, you then use a for loop and an index to apply the PygLatin to each word. As you transform each word, use the index to replace the original word with the PygLatinized word. Then after the looping is done, apply capitalize to word_list[0].

Finally, to display the transformed sentence, you can use ...

print " ".join(word_list)

#7

save me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
pyg = 'ay'
[1:len(new_word)]
original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
word = original
word = word.lower()
print word
first = word[0]
print first
else:
print 'empty'
new_word = word + first + pyg
this does not work:sob:


#8

First of all I don't know why you have that piece of code at the top. What were you trying to do with it?
If you remove that and ensure your indentations are correct (spacings of loop), you will get this:

Enter a word: Hello
hello
h
None

Is this what you were expecting?


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.