Why didn’t it work when I used a string method and then printed my string?

Question

Why didn’t it work when I used a string method and then printed my string?

Answer

Recall that variables are kind of like boxes that store values. We can think of these string methods as standalone operations that just happen and then the program moves on.
If you create a variable and on the next line use a string method, like my_string.upper(), it does not change the string stored in your my_string variable. It only performs that operation and then moves on.
If you want to actually change your stored string to uppercase, you would have to assign your string the value of itself to uppercase! Take a look at the code below for a better understanding:

my_string = “i’m a string that started as lowercase”
my_string.upper()   # This DOES NOT change the value stored in my_string
print my_string   # This will print the lowercase version since it didn’t change

# Displaying uppercase but not actually changing the value of my_string:
print my_string.upper()   # Prints uppercase but doesn’t change my_string’s value
print my_string   # Prints lowercase version since value didn’t change

# Actually changing the value of the variable:
my_string = my_string.upper()
print my_string   # Prints all uppercase version since the value was updated

10 Likes

That answer was great! Thanks

6 Likes

Actually I was trying to assign alpha numeric value to a variable. if i use double quotes (" ")its working fine but if I use str() function its showing syntax error.
Whats the limitation of str().

I believe str() can only be used to convert another data type to a string, so it cannot be used this way to create a string. Note what happens if you try this instead.

my_string = str(rajiV)
#output:
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    my_string = str(rajiV)
NameError: name 'rajiV' is not defined

A NameError occurs because the argument passed to str() is an undefined variable.

@code5911381303, @fox3773

Hello everyone, I wanted to elaborate on fox3773’s reply, hopefully some people will find this explanation useful.

It’s not really a limitation. Passing such value into str() directly is misunderstanding of how it works.

Just like you can’t do something like this:
my_string = “rajiV” + 12
because you’re trying to combine a string with an integer, you can’t just put those two values together inside str() function either, even though the function itself is supposed to convert your input into a string. What you were trying to do in your code example can be done, but you have to follow certain rules if you want to do it.

In real world scenario, you could use one of the following ways to solve this problem:

1. Pass the whole value as a string
Example:

my_string = str("rajiV 12")
print len(my_string)
print my_string.upper()

2. Pass the value through a variable - same as in the first case, you’re just using a variable
Example:

rajiV_12 = "rajiV 12"
my_string = str(rajiV_12)
print len(my_string)
print my_string.upper()

3. Combine string "rajiV " with an integer 12 converted into a string using str() function
Example:

my_string_num = 12
my_string = "rajiV " + str(my_string_num)
print len(my_string)
print my_string.upper()

First two scenarios don’t make much sense since you are already passing a string into str() function, but hey, if you wanted to do that for whatever reason, interpreter won’t complain about that… :slight_smile:

1 Like

print This was awesome . Thank u