Kodeclik Blog
What are string variables?
A string is a sequence of characters and a string variable is a variable that stores a string.
Strings and string variables are typically used for concepts that must be taken literally (i.e., without evaluation). For instance, a person’s name or address could be stored in a string variable. However, a person’s age should not be stored in a string variable because we might want to do operations on the age, e.g., determine what the age will be in 10 more years, or determine the year they are born etc. In such cases because we will be performing arithmetic operations, the age should ideally be an integer.
String variables can store really large objects such as the full text of a book or a webpage which can be viewed as merely (long) sequences of characters.
Below is some Python code that uses strings and string variables.
name = "Donald Duck"
address = "123 Disney Drive"
print(name)
print(address)
The output will be:
Donald Duck
123 Disney Drive
Note that because string variables store just sequences of characters the characters could also contain numbers, like in the address example above.
Operations on String Variables
Note that because string variables are strings you can perform operations that are reasonably defined over strings. For instance, you can concatenate strings which simply means joining the strings one after the other. Incidentally, the string concatenation operator is “+” (which means arithmetic addition when used with integers).
Below is some code that concatenates the two string variables we created above:
name = "Donald Duck"
address = "123 Disney Drive"
print(name+address)
The output is:
Donald Duck123 Disney Drive
Note that there is no space between the strings since we are just concatenating one immediately after the other. If we desire a space, we can do something like:
name = "Donald Duck"
address = "123 Disney Drive"
print(name + ' ' + address)
The output will be:
Donald Duck 123 Disney Drive
In this case we are concatenating three strings, two defined by the string variables and one defined by the string constant denoting a space.
Comparing String Variables
You can use “==” as the operator to compare two string variables in Python.
If we run the program below:
name1 = "Donald Duck"
name2 = "Donald duck"
if (name1 == name2):
print("Names are same")
else:
print("Names are different")
We will get:
Names are different
because strings are case-sensitive. Thus the two variables store two different strings.
Changing String Variables
Note that string variables are variables. This means that the values they store can change! Let us try re-assigning values to strings
name = "Donald Duck"
print(name)
name = "Mickey Mouse"
print(name)
Initially, in the above code, the string variable name initially stores the constant “Donald Duck”. Thus at the first printing of the name variable, we obtain “Donald Duck”. In the third line of code, name is now “Mickey Mouse” and thus the output printed will change accordingly:
Donald Duck
Mickey Mouse
Treating an Integer as a String
Sometimes you will have an integer variable but desire to treat it as a string variable. Let us explain why this might be needed. Consider the following code:
name = "Donald Duck"
age = 12
address = "123 Disney Drive"
print(name + " is " + age + " years old and lives in " + address)
In the above code we have a name (which is a string), an age (which is an integer), and an address (also a string). We are intending to print an informative message concatenating all these aspects. If we run the above program we will get:
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(name + " is " + age + " years old and lives in " + address)
TypeError: can only concatenate str (not "int") to str
As the error suggests we are attempting to concatenate (the “+” operator) an integer (i.e., the age variable) with strings on either side, which is not possible.
We need to “typecast”, i.e., convert the integer-valued age variable to a string before concatenation. Here is how that will work:
name = "Donald Duck"
age = 12
address = "123 Disney Drive"
print(name + " is " + str(age) + " years old and lives in " + address)
Here we are first using the str() function to convert age into a string. This is not stored in any string variable (although we could have). Instead we are just concatenating the name and address before and after it to get our message:
Donald Duck is 12 years old and lives in 123 Disney Drive
If you liked this blogpost, checkout our article on strings which will give you more practice with string operations.