- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I concatenate str and int objects in Python?
Concatenating a string and an integer in Python is a common task in programming. Here are three different ways to do it:
Using the str() function to convert the integer to a string
Example
In this method, we define a string called my_string and an integer called my_number. We use the str() function to convert my_number to a string, and then we concatenate the two strings using the + operator. Finally, we print out the resulting string.
my_string = "The answer is: " my_number = 42 result = my_string + str(my_number) print(result)
Output
The answer is: 42
Using f-strings
Example
In this method, we define a string called my_string and an integer called my_number. We use an f-string to insert the value of my_number into the string. We surround my_string and my_number with curly braces {} to tell Python to substitute the value of my_number for that expression in the resulting string. Finally, we print out the resulting string.
my_string = "The answer is: " my_number = 42 result = f"{my_string}{my_number}" print(result)
Output
The answer is: 42
Using string formatting
Example
In this method, we define a string called my_string that includes a placeholder %d for a decimal number. We then use the % operator to substitute the value of my_number for that placeholder in the resulting string. Finally, we print out the resulting string.
my_string = "The answer is: %d" my_number = 42 result = my_string % my_number print(result)
Output
The answer is: 42
Using join() method with a list comprehension
Example
In this method, we define a string called my_string and a list of integers called my_numbers. We use a list comprehension to convert each integer to a string using str(num). Then, we use the join() method to concatenate the string representation of each integer in the list with a comma and a space. Finally, we concatenate the resulting string with my_string and print out the final string.
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
The numbers are: 1, 2, 3, 4
Using string formatting with named placeholders
Example
In this method, we define a string called my_string that contains named placeholders {num_apples} and {num_oranges}. We then use the format() method to substitute the values of num_apples and num_oranges for those placeholders in the resulting string. We pass these values to the format() method as keyword arguments. Finally, we print out the resulting string.
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
I have 5 apples and 3 oranges.
Using f-strings with expressions
Example
In this method, we define an integer called num. We then use an f-string to insert expressions into the string using curly braces {}. We can perform arithmetic operations within these curly braces to generate dynamic values. In this example, we multiply num by 2 and insert the resulting value into the string, and also insert the value of num itself. Finally, we print out the resulting string.
num = 42 result = f"The answer is {num * 2}, not {num}." print(result)
Output
The answer is 84, not 42.
We have thus seen that these examples are helpful in concatenating a str object and an int object.