Concatenate String and Int in Python

How to concatenate string and int in Python? you can concatenate a string and an integer by converting the integer to a string using the str() function and then you can use the + operator to join them together.

You can concatenate string and int in Python using many ways, for example, by using str(), % operator, format(), f-strings, and print() statements. In this article, I will explain how to concatenate string and int by using all these functions with examples.

1. Quick Examples of Concatenating String and Int

If you are in a hurry, below are some quick examples of concatenating string and int.


# Quick examples of concatenating string and int

# Defining string and integer
string_value = "Welcome to SparkByExamples "
integer_value = 2023

# Example 1: Using str() method
# Concatenate string and integer 
result = string_value + str(integer_value)

# Example 2: Using % operator
# Concatenate string and integer
result = "%s%d" % (string_value, integer_value)

# Example 3: Using % operator 
result = "%s%s" % (string_value, integer_value)

# Example 4: Using format() method
# Concatenate string and integer
result = "{} {}".format(string_value, integer_value)

# Example 5: Using f-string
# Concatenate string and integer
result = f"{string_value} {integer_value}"

# Example 6: Using print() statement
# Concatenate string and integer
print(string_value, integer_value, sep="")

2. Concatenate String and Integer Using the str() & + Operator

You can concatenate strings and integers using the str() method. For example, first, define a string variable string_value with the value "Welcome to SparkByExamples" and an integer variable integer_value with the value 2023. The str() method is then used to convert the integer value to a string, and the resulting string is concatenated with the original string_value using the + operator. Concatenation means joining two or more strings together. After concatenating the string and integer, the result is stored in the variable result.


# Defining string and integer
string_value = "Welcome to SparkByExamples "
integer_value = 2023
print("Given string:", string_value)
print("Given integer:", integer_value)

# Using str() method and + operator
# Concatenate strings and integers 
result = string_value + str(integer_value)
print("After concatenating string and integer:", result)

Yields below output.

The integer value 2023 is converted to a string and then appended to the original string, the final output is a combination of the strings.

3. Concatenate String and Integer Using % Operator

You can also concatenate strings and integers using the % operator along with the %s placeholder for strings and %d placeholder for integers. In this program, the %s placeholder is used to represent the string_value, and the %d placeholder is used to represent the integer_value. Then, the % operator is used to combine the two values into a single string.


# Defining string and integer
string_value = "Welcome To SparkByExamples "
integer_value = 2023
print("Given string:", string_value)
print("Given integer:", integer_value)

# Using % operator
# Concatenate string and integer
result = "%s%d" % (string_value, integer_value)
print("After concatenating string and integer:",result)

Similarly, you can use the % operator to concatenate the string_value and integer_value. The placeholders %s are used to represent the string value, and no specific placeholder is used for the integer value, as the % operator can automatically handle integers when combined with %s.


# Using % operator 
result = "%s%s" % (string_value, integer_value)
print("After concatenating string and integer:", result)

Yields the same output as above.

4. Concatenate String and Integer Using format()

You can also concatenate strings and integers using the format() method. The format() method allows you to create formatted strings by replacing placeholders with actual values. In this program, the curly braces {} act as placeholders. The format() method replaces these placeholders with the values provided inside the format() function in the order they appear. In this case, the first {} is replaced by string_value, and the second {} is replaced by integer_value.


# Defining string and integer
string_value = "Welcome To SparkByExamples"
integer_value = 2023
print("Given string:", string_value)
print("Given integer:", integer_value)

# Using format() method
# Concatenate string and integer
result = "{} {}".format(string_value, integer_value)
print("After concatenating string and integer:", result)

Yields the same output as above.

5. Concatenate String and Integer Using f-strings

You can also use f-strings (formatted string literals) to provide a concise and readable way to concatenate strings and integers. You can directly embed expressions inside curly braces {} within the string, and the expressions will be evaluated and substituted with their values. In this program, the f prefix before the string indicates that it’s an f-string. The expressions inside the curly braces {} are evaluated and replaced with their corresponding values.


# Defining string and integer
string_value = "Welcome To SparkByExamples"
integer_value = 2023
print("Given string:", string_value)
print("Given integer:", integer_value)

# Using f-string
# Concatenate strings and integers
result = f"{string_value} {integer_value}"
print("After concatenating string and integer:", result)

Yields the same output as above.

6. Concatenate String and Integer Using print() Statement

You can concatenate strings and integers using the print() statement. The print() statement allows you to display text and values together in the output.

In the below example, the print() statement takes multiple arguments separated by commas. The text and variables are separated by commas, and print() automatically concatenates them when displaying the output.


# Defining string and integer
string_value = "Welcome To SparkByExamples "
integer_value = 2023

# Using print() statement to concatenate strings and integers
print("After concatenating string and integer:", string_value, integer_value, sep="")

Yields the same output as above.

Conclusion

In this article, I have explained how to concatenate string and integer in Python by using the str(), % operator, format(), f-strings, and print() statements with examples.

Happy Learning !!

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply