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.
The output will be:
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:
The output is:
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:
The output will be:
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:
We will get:
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
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:

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:
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:
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:
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:
If you liked this blogpost, checkout our article on strings which will give you more practice with string operations.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.