The function title()
is a function that is associated with str
of Python, that is, you can only use it if it is in a string. If you have a list of strings and wants each of the list to be capitalized with the function title
, you have to scroll through the list and add the function title
for each of them, this serves for any interfacing in Python, as list
, set
, tuple
, etc. A simple way to do this without having to create a block for
to go through your strings is using the comprehensions of Python, it would look like this.
lista =['laranja', 'mamão', 'maçã']
nova_lista_captalizada = [ i.title() for i in lista]
tupla = ('banana', 'mamão', 'maçã')
nova_tupla = tuple( i.title() for i in tupla)
# O modo de cima é a mesma coisa que a debaixo
nova_lista_captalizada =[]
for i in lista:
nova_lista_captalizada.append(i.title())
If you still don’t know the comprehensions in Python, no problem, you need to understand here, that function title()
, is related to str
in Python.
The words
list
,tuple
andset
are built-in (built-in Function) python functions. Do not use these words as variables.– AlexCiuffa