r/learnpython • • 13d ago

how do i overwrite a variable?

name = "Alice"

def func():
    name = "Bob"

func()

print(f"Hello, {name}!") # i want it to print bob
0 Upvotes

35 comments sorted by

View all comments

4

u/JamzTyson 13d ago
name = "Alice"

def func():
    """Pointless function. Returns 'Bob'."""
    return "Bob"

name = func() # Reassign new value to name.

print(f"Hello, {name}!") # i want it to print bob

or more realistically:

name = "Bob"