Python Tutorial: How To Concatenate in Python? #45



See our full tutorial at
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.

Nguồn: https://benjaminjcohen.com/

Xem thêm bài viết khác: https://benjaminjcohen.com/cong-nghe/

10 Comments

  1. print("your birthday is " + str(res))

  2. how to concatenate value within the string

  3. Can you tell me if we have given '<<>>' and Bob and if i want to add Bob inbtwn these tags would it happen

  4. you are very good at explaining things!

  5. thats good but stop tricking people to come here. wish u luck -_-

  6. Very helpful stuff. Thank you

  7. How would I concatenate integer or float variables

  8. Hello I have a problem and I'm looking for the answer, if you have it could you help me thanks. So I'm coding an AI and i have it recognizing ("hi") but if i say any sentence with ("this")in it, it will respond with hi because it see's hi in this is there a way to tell python to only see hi if its naked to only hi and does not have other characters connected to it but spaces. like hi not t-h-i-s

  9. Hello I have a problem and I'm looking for the answer, if you have it could you help me thanks. So I'm coding an AI and i have it recognizing ("hi") but if i say any sentence with ("this")in it, it will respond with hi because it see's hi in this is there a way to tell python to only see hi if its naked to only hi and does not have other characters connected to it but spaces. like hi not t-h-i-s

Leave a Reply

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