How to Concatenate String and Integer in Python?

In this Python tutorial, we will learn how to concatenate strings and integers.

Table Of Contents

Concatenate strings and integers using str()

The str() function is used to convert the integer into a string. So after converting the integer to a string, we can concatenate the string and integer using + operator.

Syntax:

string + str(integer)

Here, a string is the input string and an integer is the integer value to be concatenated with the string.

Example 1:

# Consider the string
string1 = "Welcome to thisPointer"

# Consider the integer
value = 2022

# Concatenate string1 and value
newStr = string1 + str(value) 

print(newStr)

Output:

Welcome to thisPointer2022

Here, 2022 is the integer that is concatenated with the string – “Welcome to thisPointer”.

Example 2:

If you need space between strings and integers, you can use separator – ” “.

# Consider the string
string1="Welcome to thisPointer"

# Consider the integers
value1=2022
value2=12
value3=3

# Concatenate string1 and values
newStr = string1 + str(value1)+ str(value2)+ str(value3)

print(newStr)

# Concatenate string1 and values with space
newStr = string1 + " " + str(value1) + " " + str(value2) + " " + str(value3)

print(newStr)

Output:

Welcome to thisPointer2022123
Welcome to thisPointer 2022 12 3

Here, three integers are concatenated with the string – “Welcome to thisPointer” with and without spaces.

Concatenate strings and integers using % operator

%s is used to specify the string and %d is used to specify the integer type. So if we keep $s and %d together, we can concatenate string and integer.

Syntax:

"%s%d" % (string, integer)

Here, the string is the input string and integer is the integer value to be concatenated with the string.

Example 1:

# Consider the string
string1="Welcome to thisPointer"

# Consider the integer
value=2022

# Concatenate string1 and value
newStr = "%s%d" % (string1, value)

print(newStr)

Output:

Welcome to thisPointer2022

Here, 2022 is the integer that is concatenated with the string – “Welcome to thisPointer”.

Example 2:

If you need space between strings and integers, you can leave space between each specifier.

# Consider the string
string1="Welcome to thisPointer"

# Consider the integers
value1=2022
value2=12
value3=3

# Concatenate string1 and values
newStr = "%s%d%d%d" % (string1, value1,value2,value3) 

print(newStr)

# Concatenate string1 and values with space
newStr = "%s %d %d %d" % (string1,value1,value2,value3)

print(newStr)

Output:

Welcome to thisPointer2022123
Welcome to thisPointer 2022 12 3

Here, three integers are concatenated with the string – “Welcome to thisPointer” with and without spaces.

Concatenate strings and integers using format()

The format() function concatenates the variables in python using {} specifier. So we can concatenate string and integer.

Syntax:

"{}{}.format(string,value)

Here, the string is the input string and integer is the integer value to be concatenated with the string.

Example 1:

# Consider the string
string1="Welcome to thisPointer"

# Consider the integer
value=2022

# Concatenate string1 and value
newStr = "{}{}".format(string1, value)

print(newStr)

Output:

Welcome to thisPointer2022

Here, 2022 is the integer that is concatenated with the string – “Welcome to thisPointer”.

Example 2:

If you need space between strings and integers, you can leave space between each specifier.

# Consider the string
string1 = "Welcome to thisPointer"

# Consider the integers
value1 = 2022
value2 = 12
value3 = 3

# Concatenate string1 and values
newStr = "{}{}{}{}".format(string1, value1,value2,value3)

print(newStr)

# Concatenate string1 and values with space
newStr = "{} {} {} {}".format(string1, value1,value2,value3)

print(newStr)

Output:

Welcome to thisPointer2022123
Welcome to thisPointer 2022 12 3

Here, three integers are concatenated with the string – “Welcome to thisPointer” with and without spaces.

Concatenate strings and integers using f-strings

The f strings take two or more strings and concatenate them. It is possible to place the integers after the strings so that we can concatenate strings and integers.

Syntax:

f'{string}{value}'

Here, the string is the input string and integer is the integer value to be concatenated with the string.

Example 1:

# Consider the string
string1="Welcome to thisPointer"

# Consider the integer
value=2022

# Concatenate string1 and value
newStr = f'{string1}{value}'

print(newStr)

Output:

Welcome to thisPointer2022

Here, 2022 is the integer that is concatenated with the string – “Welcome to thisPointer”.

Example 2:

If you need space between strings and integers, you can leave space between each variable.

# Consider the string
string1="Welcome to thisPointer"

# Consider the integers
value1=2022
value2=12
value3=3

# Concatenate string1 and values
print(f'{string1}{value1}{value2}{value3}')

# Concatenate string1 and values
print(f'{string1} {value1} {value2} {value3}')

Output:

Welcome to thisPointer2022123
Welcome to thisPointer 2022 12 3

Here, three integers are concatenated with the string – “Welcome to thisPointer” with and without spaces.

Concatenate strings and integers within print() statement

Using a separator, we can concatenate string and integers within a single print() statement. It is important to separate each variable using a comma operator

Syntax:

print(string, value, sep="")

Here, the string is the input string and the integer is the integer value to be concatenated with the string.

Example 1:

# Consider the string
string1="Welcome to thisPointer"

# Consider the integer
value=2022

# Concatenate string1 and value
print(string1, value, sep="")

Output:

Welcome to thisPointer2022

Here, 2022 is the integer that is concatenated with the string – “Welcome to thisPointer”.

Example 2:

If you need space between strings and integers, you can assign a space(” “) to the sep parameter.

# Consider the string
string1="Welcome to thisPointer"

# Consider the integers
value1=2022
value2=12
value3=3

# Concatenate string1 and values
print(string1, value1,value2,value3 ,sep="")

# Concatenate string1 and values
print(string1, value1,value2,value3 ,sep=" ")

Output:

Welcome to thisPointer2022123
Welcome to thisPointer 2022 12 3

Here, three integers are concatenated with the string – “Welcome to thisPointer” with and without spaces.

Summary

In this article, we see five different ways to concatenate strings and integers with examples in detail. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top