HDTutorial
EN

Playback starts YouTube’s embedded player (privacy-enhanced mode).

Python Tutorial: How To Concatenate in Python? #45

27,675 views October 29, 2015 6:19

Watch on YouTube
ChannelMaster Code Online

Description from the creator

See our full tutorial at learnpythontutorial.com/how-to-concatenate-in-p…
How to Concatenate in Python?
In this Python tutorial, we will look at how to concatenate in Python. There are occasions when we would want to combine one or more strings together and this is called concatenation in programming. We can perform concatenation in Python by adding a plus(+) symbol between strings. When we concatenate you may want to plan ahead and add a space at the end of the first string or add a space at the beginning of the second string otherwise our strings will be combined with no space. Another solution would be to create another string that contains a space.

Examples of Concatenation in Python
#Combine two strings notice space in the beginning of second string
'This is the first string' + ' This is the second string'
'This is the first string This is the second string'

#Adding a space in concatenation
'We can also add a space to a string like this' + ' ' + 'Now we have a space'
'We can also add a space to a string like this Now we have a space'

# Concatenating variables but notice no space fix that in the next example
a = 'This is the first string'
b = 'This is the second string'
print(a + b)
This is the first stringThis is the second string

#Fixing the space from the above example
a = 'This is the first string'
b = 'This is the second string'
c = ' ' #there is a space in this string
print(a + c + b)
This is the first string This is the second string

#How about making a new object for future use
d = a + c + b
print(d)
This is the first string This is the second string
We can not concatenate a string to any other type of object on strings. For example we cannot concatenate a string 'This is a string' + 9 this will return an error. In this case we would need to convert the 9 integer to a string for the concatenation to work. Look at the example below.

a = 'This is a string'
b = 9
a + b
Traceback (most recent call last):
File "stdin", line 1, in module
TypeError: Can't convert 'int' object to str implicitly
a + str(b)
'This is a string9'
If you have any questions about how to concatenate in Python leave a comment below and we will help you out.

About this tutorial

Master Code Online published this 6:19 video tutorial on October 29, 2015. It has been watched 27,675 times on YouTube.

Category
Education
Duration
6:19
Published
October 29, 2015
Views
27,675
Likes
150

Tags

Share

This tutorial in other languages