Javatpoint Logo
Javatpoint Logo

How to Concatenate a String and Integer in Python?

Firstly, we can use the inbuilt str() function to concatenate an integer and a string in Python which takes an integer as an argument and results in a string, and both these strings can be combined using the "+" operator.

It can be implemented as follows:

Output:

I am 28 years old.

Explanation:

In this example, we first create a variable age with an integer value of 28. After that, we create a string variable message that includes the text "I am " and the integer value of age. To concatenate the integer and string, we use the str() function to convert the integer to a string and then use the + operator to concatenate the two strings. For example, str (27) returns the string '27'. Finally, we print out the message variable.

  • Using f-strings (formatted string literals)

We can also use f-strings (formatted string literals) in Python to concatenate a string and an integer more easily, which can be implemented as follows:

Output:

I am 18 years old.

Explanation:

In this example, we use the f-string syntax to include the variable age within the string using curly braces {}. The age variable is automatically converted to a string and included within the string when the code is run. This code would produce the same result as the preceding example.

  • Using the approach of String formatting

Another approach to concatenate a string with an integer is using the approach of String formatting. Let's take an example to understand this method.

Code:

Output:

Hello 42
  • Using Concatenation with a comma

Another approach to concatenate a string with an integer is using concatenation with comma. Let's take an example to understand this method.

Code:

Output:

Hello 42

Explanation:

In this example, we use a comma to separate the values of string_variable and integer_variable. The print() function automatically adds a space between the values, resulting in the output "Hello 42".

Note: This approach does not actually concatenate the values into a single string.

  • Using template strings

You can use template strings to insert the integer value into a string. Let's take an example to understand this method.

Code:

Output:

Hello, 42

Explanation:

In this example, we create a template string that contains placeholders for the string and integer values. After that, we use the substitute() method to insert the values into the placeholders and create the final string value "Hello, 42".

  • Using % Operator

You can use the % operator to format a string and insert the integer value into it. Let's take an example to understand this.

Code:

Output:

Hello 42

Explanation:

In this example, we use %s to represent the string value and %d to represent the integer value. After that, we use the % operator to replace these placeholders with the values of string_variable and integer_variable. The resulting string value is "Hello 42".

  • Using bytearray

You can also use a bytearray to concatenate a string and an integer in Python. Let's take an example to understand this method.

Code:

Output:

Hello42

Explanation:

In this example, we first convert the string value to a bytearray using the bytearray() function. After that, we use the extend() method to add the byte representation of the integer value to the bytearray. Finally, we convert the bytearray back to a string using the decode() method, resulting in the string value "Hello42".

  • Using the + operator with a formatted integer

You can also use the + operator with a formatted integer. Let's take an example to understand it.

Code:

Output:

Hello 42






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA