cristiano ronaldo
cristiano ronaldo

Reputation: 172

Problem with UpperCase, str object has no attribute 'isUpperCase'

I want the program to change the characters of a string object from lower to capital and vice versa.

So that if I input jOHN, it returns John.

Here I leave you the code:

nombre=input('Write the wrong name here: ')

nuevonombre=''
contador=0
while contador <= len(nombre):
    if nombre[contador].isUpperCase == true:
        nombre[contador].lower()
    else:
        nombre[contador].upper()
    nuevonombre = nuevonombre + nombre[contador]
    contador+=1
print(nuevonombre)

But when I run the program, the terminal gives the following error:

if nombre[contador].isUpperCase == true:
AttributeError: 'str' object has no attribute 'isUpperCase'

Maybe I shouldn't use .isUpperCase, if that's not it what should I do? Thanks.

Upvotes: 0

Views: 2417

Answers (3)

Gabio
Gabio

Reputation: 9494

a bit shorter solution using list comprehension:

input_name = input('Write the wrong name here: ')
res = "".join([x.lower() if x.isupper() else x.upper() for x in list(input_name)])
print(res)

where list converts your input to list of chars, and join converts it back to string.

Upvotes: 0

Telcrome
Telcrome

Reputation: 369

There is no function isUpperCase in python, it is called 'john'.isupper().

Your problem can be solved much simpler using pythons slicing syntax and simple string functions:

def fix_str(s: str) -> str:
    return s[0].upper() + s[0:].lower()

>>> fix_str('jOhN')
'John'

Upvotes: 1

SMortezaSA
SMortezaSA

Reputation: 589

You should use isupper() method.

if nombre[contador].isupper():

Upvotes: 1

Related Questions