
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Concatenate str and int Objects in Python
In many coding languages, when you try to combine a string with a number, the number is usually converted into a string automatically so both parts can be joined together. This is called implicit type conversion.
With the + operator for contaminating a string with a numerical value, Python does not perform the implicit type conversion. Instead, it throws an error (specifically a TypeError) because it expects both operands to be strings. The following are the various methods to do it:
- Using the str() Function
- Using f-strings
- Using join() method with a list comprehension
- Using string formatting with named placeholders
Using the str() Function
The str() function converts an integer to a string. This allows you to concatenate it with another string using the + operator.
Example
In the following example, we converted the integer (42) into a string using the str() function and concatenated the two strings using the + operator.
my_string = "The answer is: " my_number = 42 result = my_string + str(my_number) print(result)
Output
Following is the output of the above code ?
The answer is: 42
Using f-strings
F-strings, or formatted string literals, allow us to embed expressions inside string literals by prefixing them with f. We can directly insert variables and expressions within curly braces {} inside the f-string.
Example
This example demonstrates joining a string and an integer using an f-string. The number inside the curly braces {} is automatically converted into a string and combined with the string("Hello").
my_string = "Hello" my_number = 123 result = f"{my_string}{my_number}" print(result)
Output
Following is the output of the above code ?
The answer is: 42
Using join() Method with a List Comprehension
Python's .join() method, method is useful when combining multiple values, especially in a list. When used with a list comprehension, each item (like numbers) can be turned into strings and joined with a separator like a comma. We can create a modified list for joining.
Example
This example shows how to combine a list of numbers with text. Each number is first turned into a string and then joined together with commas using .join(). Finally, this combined string is added to the text.
my_string = "The numbers are: " my_numbers = [1, 2, 3, 4] result = my_string + ", ".join(str(num) for num in my_numbers) print(result)
Output
Following is the output of the above code -
The numbers are: 1, 2, 3, 4
Using string formatting with named placeholders
This method improves readability by assigning names to placeholders in a string. The .format() function then replaces these with actual values.
For instance, if we write placeholders like {name} in the string and use format(name="value") to fill them. This makes the code more readable and easier to understand, especially when working with multiple values.
Example
This example demonstrates how to use named placeholders in a string. The placeholders are replaced with actual values using the .format() method.
my_string = "I have {num_apples} apples and {num_oranges} oranges." num_apples = 5 num_oranges = 3 result = my_string.format(num_apples=num_apples, num_oranges=num_oranges) print(result)
Output
Following is the output of the above code -
I have 5 apples and 3 oranges.