A string is a sequence of one or more characters (letters, numbers, symbols). Strings are a common form of data in computer programs, and we may need to convert strings to numbers or numbers to strings fairly often, especially when we are taking in user-generated data.
We can convert numbers to strings through using the str() method. We’ll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value.
Let’s first look at converting integers. To convert the integer 12 to a string value, you can pass 12 into the str() method:
When running str(12) with the python command in a terminal window, you’ll receive the following output:
The quotes around the number 12 signify that the number is no longer an integer but is now a string value.
With variables we can begin to see how practical it can be to convert integers to strings. Let’s say we want to keep track of a user’s daily programming progress and are inputting how many lines of code they write at a time. We would like to show this feedback to the user and will be printing out string and integer values at the same time:
user = "Sammy" lines = 50 print("Congratulations, " + user + "! You just wrote " + lines + " lines of code.")
When we run this code, we receive the following error:
TypeError: Can't convert 'int' object to str implicitly
We’re not able to concatenate strings and integers in Python, so we’ll have to convert the variable lines to be a string value:
user = "Sammy" lines = 50 print("Congratulations, " + user + "! You just wrote " + str(lines) + " lines of code.")
Now, when we run the code, we receive the following output that congratulates our user on their progress:
Congratulations, Sammy! You just wrote 50 lines of code.
If we are looking to convert a float to a string rather than an integer to a string, we follow the same steps and format. When we pass a float into the str() method, a string value of the float will be returned. We can use either the float value itself or a variable:
print(str(421.034)) f = 5524.53 print(str(f))
We can test to make sure it’s right by concatenating with a string:
f = 5524.53 print("Sammy has " + str(f) + " points.")
We can be sure our float was properly converted to a string because the concatenation was performed without error.
Strings can be converted to numbers by using the int() and float() methods.
If your string does not have decimal places, you’ll most likely want to convert it to an integer by using the int() method.
Let’s use the example of the user Sammy keeping track of lines of code written each day. We may want to manipulate those values with math to provide more interesting feedback for the user, but those values are currently stored in strings:
lines_yesterday = "50" lines_today = "108" lines_more = lines_today - lines_yesterday print(lines_more)
Because the two numeric values were stored in strings, we received an error. The operand - for subtraction is not a valid operand for two string values.
Let’s modify the code to include the int() method that will convert the strings to integers, and allow us to do math with values these that were originally strings.
lines_yesterday = "50" lines_today = "108" lines_more = int(lines_today) - int(lines_yesterday) print(lines_more)
The variable lines_more is automatically an integer, and it is equal to the numeric value of 58 in this example.
We can also convert the numbers in the example above to float values by using the float() method in place of the int() method. Instead of receiving the output of 58, we’ll receive the output of 58.0, a float.
The user Sammy is earning points in decimal values
total_points = "5524.53" new_points = "45.30" new_total_points = total_points + new_points print(new_total_points)
In this case, using the + operand with two strings is a valid operation, but it is concatenating two strings rather than adding two numeric values together. So, our output looks unusual since it just places the two values next to each other.
We’ll want to convert these strings to floats prior to performing any math with the float() method:
total_points = "5524.53" new_points = "45.30" new_total_points = float(total_points) + float(new_points) print(new_total_points)
Now that we have converted the two strings to floats, we receive the anticipated result that adds 45.30 to 5524.53.
If we try to convert a string value with decimal places to an integer, we’ll receive an error:
f = "54.23" print(int(f))
If we pass a decimal value in a string to the int() method we’ll receive an error because it will not convert to an integer.
Converting strings to numbers enables us to quickly modify the data type we are working with so that we can perform operations on numeric values that were originally cast as strings.
Write a program to demonstrate converting an String to a Number
Write a program to demonstrate converting an String to a Number and a Number to a String
Write a program to concatenate 2 numbers.
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
Have a great day!!
K McGuinness - 2018
No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.