TLuna7
TLuna7

Reputation: 9

How to find lowercase first letter in word list, and change them into uppercase

I want to find out if any of those names start with lowercase and, if they do, change it to uppercase.

unknown_list = ('toby', 'James', 'kate', 'George', 'rick', 'Alex', 'Jein', 'medelin')

Upvotes: 0

Views: 267

Answers (5)

slovik
slovik

Reputation: 77

One small reminding is that the parentheses you're using make your unknown_list a tuple. Tuples are immutable.

If you just want to capitalize everything in the list you can do this

unknown_list = ['toby', 'James', 'kate', 'George', 'rick', 'Alex', 'Jein', 'medelin']
capLs = []
for i in unknown_list:
  capLs.append(i.capitalize())
print(capLs)

Upvotes: 0

Ginta
Ginta

Reputation: 151

maybe you can do like this:

x = ['toby', 'James', 'kate', 'George', 'rick', 'Alex', 'Jein', 'medelin']
x = [name.title() for name in x]

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881083

The capitalize() method can do this easily:

>>> unknown_list = ('toby', 'James', 'kate', 'George', 'rick', 'Alex', 'Jein', 'medelin')

>>> new_list = [x.capitalize() for x in unknown_list]
>>> new_list
['Toby', 'James', 'Kate', 'George', 'Rick', 'Alex', 'Jein', 'Medelin']

Note that's creating a new list but you could just as easily assign back to the original variable if you want to overwrite it.

Upvotes: 1

Hindsight
Hindsight

Reputation: 11

You used to chr() and ord()

chr(97) is 'a', chr(65) is 'A'

and

ord('a') is 97 , ord('A') is 65 as int

test case:

for name in unknown_list:
    if ord(name[0]) >=97 and ord(name[0]) <=122:
        tmp = ord(name[0]) - 32
             print(chr(tmp))

But, easier way

name = 'james'
print(name.capitalize())

It can print 'James'

Upvotes: 0

Vassilios
Vassilios

Reputation: 493

Tuples are immutable so you cannot change them but if you change unknown_list into a list then you are able to do that. You should use the .capitalize() function!

Here is the short version.

x = ['toby', 'James', 'kate', 'George', 'rick', 'Alex', 'Jein', 'medelin']
x = [name.capitalize() for name in x]

And the long version.

x = ['toby', 'James', 'kate', 'George', 'rick', 'Alex', 'Jein', 'medelin']
for index, name in enumerate(x):
     x[index] = name.capitalize()

The main idea in both is that you capitalize every name in order to achieve your goal.

Upvotes: 3

Related Questions