89DEVs

Python: How to insert variable into string?

How to insert variables into strings?

How to insert a variable into a string in Python? Like always, there are many ways to reach the goal. Which way to choose, depends on your personal preferences.

string concatenation

One option is to use string concatenation, to insert a variable into a string. Make sure the variable is a string or use the str() function to convert it to string first. # insert variable into string using string concatenation myInt = 5 myString = 'High' print(myString + ' ' + str(myInt)) In this example, the variable myInt is set to the integer value 5. The variable myString is set to "High". Then, the string is printed by concatenating the variable myString, a space character, and the myInt variable. To prevent an error, the myInt variable is first converted to a string, before it is concatenated with the other values. Finally, the concatenated string is printed and the variable is inserted into the string. High 5 It is important to remember: Only string and string can be concatenated. The concatenation of string and integer values does not work. Use the str() function to convert the integer to string. If the str() function is not used to convert the variable to a string, a TypeError exception is raised. TypeError: can only concatenate str (not "int") to str Python can only concatenate strings with strings.

format method

The second option is to use the format method of strings to create a new string with the given values. # insert variable into string using format method myInt = 5 myString = 'High {}'.format(myInt) print(myString) In this example, the integer value 5 is assigned to the variable myInt. Then, the format() method is used to insert the integer value into the string 'High {}'. The curly brackets are the placeholders, that will be replaced by the integer value myInt. It is important not to forget the curly brackets, because otherwise no value is inserted into the string. Finally, the inserted string is printed and the result is 'High 5' as shown below. High 5 To insert multiple values, use multiple curly brackets and arguments for the format() method. The advantage of using this option is that you can define more specific formatting at the same time. For example, we can define that the inserted variable is formatted as a float with 3 decimals using {:.3f}

f-strings

The third option is only available in Python 3.6 and above. f-strings tell Python to replace the variable name with the variable value. This solution is quite similar to the format method shown above. # insert variable into string using f-strings myInt = 5 myString = f'High {myInt}' print(myString) The integer value 5 is assigned to the variable myInt. Then, the variable myString is declared and f strings are used to insert the myInt into the string. Note, that f strings have an f before the actual string and the curly brackets are replaced by the value of the variable inside of it. Finally, the inserted string is printed and the result is as expected. High 5

% operator

The fourth option to insert a variable into a string is to use the % operator. When using the % operator: %s is used to insert a string and %d is used to insert an integer. # insert variable into string using % operator myInt = 5 myString = 'High %d' % myInt print(myString) In this example, the variable myInt contains the integer value 5 as in the other examples above. Then myString is declared and the string 'High %d' followed by the % operator and the variable myInt is assigned to it. In this case, the %d is used, because an integer value is inserted into the string. Finally, the inserted string is printed and the result is High 5. High 5

                
        

Summary


Click to jump to section