All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Concatenate Strings In Python

Last Updated : Mar 11, 2024

How To Concatenate Strings In Python

In this article we will show you the solution of how to concatenate strings in python, a String in Python consists of a number of bytes representing Unicode characters. Characters in Python are represented by strings of length one, as the language does not support a character data type.

Accessing elements of the string is possible using square brackets [].

For string concatenation, the + operator is very convenient. Adding multiple strings together can be accomplished using this operator.

There is only one requirement, however, and that is that the arguments must be strings.

A combined string is formed by combining the strings in variables var1 and var2 into variable var3. We will now discuss the idea of how to concatenate strings in python with an example.

Step By Step Guide On How To Concatenate Strings In Python :-

Example 1

# Python program to show
# string concatenation
str1 = "Hello"
str2 = "TalkersCode"
# format function is used here to
# concatenate the string
print("{} {}".format(str1, str2))
# store the result in another variable
str3 = "{} {}".format(str1, str2)
print(str3)

Example 2

title = 'TalkersCode'
firstName = 'Amruta'
lastName = 'Shahane'
fullNameByAdditionOperator = title+firstName+lastName
fullNameByJoinMethod = ' '.join([title, firstName, lastName])
fullNameByFormatFunction = '{}-{} {}'.format(title, firstName, lastName)
print('full Name By Addition Operator: ', fullNameByAdditionOperator)
print('full Name By Format Function', fullNameByFormatFunction)
print('full Name By Join Method', fullNameByJoinMethod)
  1. Initialize two string variables, str1 and str2 with the values "Hello" and "TalkersCode" respectively.
  2. Use the format() function to concatenate the two strings using the curly braces {} as placeholders. The two string variables are passed as arguments to the format() function.
  3. The print() function is used to display the concatenated string in the console.
  4. Now, store the concatenated result in a new variable str3. Here, str3 variable contains "Hello TalkersCode".
  5. Finally, use the print() function to display the value stored in str3.

Conclusion :-

As a result, we have successfully learned how to concatenate strings in python with an example.

Python String formatting can be done in several ways.

Use them based on your requirements. If you have to concatenate a sequence of strings with a delimited, then use join() function.

If some formatting is also required with concatenation, then use format() function of-string.

Note that f-string can be used with Python 3.6 or above versions. The purpose of this article is to show you how to combine strings in Python using concatenation.

I hope this article on how to concatenate strings in python helps you and the steps and method mentioned above are easy to follow and implement.