r/pythonhelp • • Aug 02 '20

Title.fuction? HELP PLEASE

Post image
3 Upvotes

19 comments sorted by

2

u/iampratikthorat Aug 02 '20

Specify the index. Like music[1].title() it might work

1

u/TheDevine13 Aug 02 '20

Thank you!

2

u/wentaas Aug 02 '20

if you want to get every element in the list to be title, you need a for loop

musicTitles = [] # create an empty list we’ll populate it under for str in music: # takes every element in music so str is first “kay”, then “em” and so on musicTitle = str.title() # gets for example Kay musicTitles.append(musicTitle) # adds it to the list which we’ll use

print(musicTitles)

2

u/wentaas Aug 02 '20

another way to do it is to replace the list itself

i = 0 # this will be our index for str in music: # same thing as above music[i] = str.title() # music[0] = str.title() would replace the first element with the title i += 1 # and i will be first 0 then 1 and so on for every element

print(music)

2

u/wentaas Aug 02 '20

for printing lists beautifully you could print inside that loop so for str in music: str = str.title() print(str)

or print(*musicTitles) would print like Kay Em etc print(*musicTitles, sep=“, “) would do Kay, Em etc you can replace sep with whatever you like

2

u/TheDevine13 Aug 02 '20

Thank you!

1

u/TheDevine13 Aug 02 '20

Thank you to everyone that helped me get this working. As a total boob trying to teach myself coding from a book, I'll probably be back soon😂

1

u/NamasteWager Aug 02 '20

What exactly are you trying to do?

1

u/TheDevine13 Aug 02 '20

Just trying to add the title function and it errors and I have no idea why

1

u/NamasteWager Aug 02 '20

Oh I see now. You are calling it against the entire list, not against a string. Try music[0].title()

If you want to print the whole list, you will have to write a loop to do so

1

u/TheDevine13 Aug 02 '20

Ik I can do it that way but I've put in this coding like this before and it worked so I'm not sure what the difference was

1

u/NamasteWager Aug 02 '20

Music is a list, and a list does not have the title function. Make music = 'em' and it will work

1

u/TheDevine13 Aug 02 '20

This makes sense but idk how I did something like this before

1

u/TheDevine13 Aug 02 '20

However I just tried it and that didn't work either lol

1

u/NamasteWager Aug 02 '20

What was the new error

1

u/TheDevine13 Aug 02 '20

Same as before. "List" object has no attribute"title"

1

u/NamasteWager Aug 02 '20

You are still calling the list music. Make a new variable called musictemp, set it to 'test' and then try musictemp.title()

1

u/TheDevine13 Aug 02 '20

That worked, thank you!

1

u/TheDevine13 Aug 02 '20

I'll try that!