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 strings and integers
result = string_value + str(integer_value)
# Example 2: Using % operator
# Concatenate strings and integers
result = “%s%d” % (string_value, integer_value)
# Example 3: Using % operator
result = “%s%s” % (string_value, integer_value)
# Example 4: Using format() method
# Concatenate strings and integers
result = “{} {}”.format(string_value, integer_value)
# Example 5: Using f-string
# Concatenate strings and integers
result = f”{string_value} {integer_value}”
# Example 6: Using print() statement
# Concatenate strings and integers
print(string_value, integer_value, sep=””)
2. Concatenate Strings and Integers Using the str() Method
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
# Using str() method
# Concatenate strings and integers
result = string_value + str(integer_value)
print(“After concatenating strings and integers:”,result)
Yields below output.
The integer value 2023 is converted to a string and then appended to the original string, resulting in the combined string output.
3. Concatenate Strings and Integers 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
# Using % operator
# Concatenate strings and integers
result = “%s%d” % (string_value, integer_value)
print(“After concatenating strings and integers:”,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 strings and integers:”, result)
Yields the same output as above.
4. Concatenate Strings and Integers 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
# Using format() method
# Concatenate strings and integers
result = “{} {}”.format(string_value, integer_value)
print(“After concatenating strings and integers:”, result)
Yields the same output as above.
5. Concatenate Strings and Integers 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
# Using f-string
# Concatenate strings and integers
result = f”{string_value} {integer_value}”
print(“After concatenating strings and integers:”, result)
Yields the same output as above.
6. Concatenate Strings and Integers 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 strings and integers:”, string_value, integer_value, sep=””)
Yields the same output as above.
Conclusion
In this article, I have explained how to concatenate string and int in Python by using the str(), % operator, format(), f-strings, and print() statements with examples.
Happy Learning !!
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(), % Read More Python, Python Tutorial