keyx
keyx

Reputation: 587

Function to create lower case words (convert upper to lower letters) doesn't work as expected

I don't see why the following Python code prints out a sentence containing UPPER letters... Please explain! :)

def lower(text):
    text = text.lower()

example = "This sentence has BIG LETTERS."
lower(example)
print(example)

Output will be:

This sentence has BIG LETTERS.

Upvotes: 2

Views: 134

Answers (2)

Kasravnd
Kasravnd

Reputation: 107287

Your function doesn't return anything , you need to return the word :

def lower(text):
    text = text.lower()
    return text

Demo:

>>> example = "This sentence has BIG LETTERS."
>>> lower(example)
'this sentence has big letters.'

if you mean why the following doesn't work :

lower(example)
print(example)

you need to know that variables inside the functions has local scope and with call lower(example) the example doesn't change globally !

Upvotes: 2

meda
meda

Reputation: 45490

Use built in functions, directly:

print example.lower()

Don't wrap it or reinvent it !

Upvotes: 0

Related Questions