How To Concatenate Strings In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up everybody my name is zach welcome back to case digital and in this video we're going to answer the question how can i concatenate strings in python so without further ado let's hop right in and start coding what is up everybody so like i mentioned this video is all about helping you to answer the question of how you can concatenate strings in python but for those who don't know what concatenate means or how to concatenate strings we're first going to cover that definition and then we're going to jump into five different ways that you can concatenate strings in python so without further ado let's hop right in and start coding so for those who don't know concatenation or combining is essentially the other name for it strings together in python is exactly that it's just taking one string and combining with another to make a third so basically say i have a string i'm going to assign variables so i have let's go with the hello string hello and then i have a world string and say i want to combine these two together to essentially make my main string equal to hello world so how do i go about doing that because that's that's essentially what concatenation is is how do i take this one string and combine it with this other to make this third that is simply what we are doing here today that is that is the essence of this question how can i concatenate strings in python well there are several actually different ways to do this and i'm going to show you five different ways today the first one is using the plus operator basically it's just like math a plus b equals c but essentially what i could do and just to print this out and show you that it works i'm gonna do print and i'm gonna do hello plus world and if i go down here and i do python python strings up hi and run it i'm gonna get oh hello world but wait a minute it's hello no space and then world so that's one thing to watch out for is when you do string concatenation when you combine them you need to make sure the strings themselves have all the formatting you want because what did we want we wanted hello hello space world and in essence we got hello world one word so to do that what i could do is i could either add a space here or i could add a space here and if i run this i'll get my hello world another thing that i could do is if i didn't want to mess with these is i could add another concatenation sequence here and add basically to it basically take hello if i go back here if i go back here and i say hello plus empty space plus world that should actually give us the same thing of hello world so that is method one and that is just basically using the plus operator so next we're going to dive into join and join i mean i'll probably end up doing a whole video on join because there's a lot that you can do with it but essentially what i could do is if i wanted to create another string i could do with join join deals with lists so essentially when you're doing joining with strings i'll give you the syntax here first so when i do join with the string i have to give i have to pass in a list of different strings so what i could do is i could do a list so i create a list by using the brackets i type in hello and then comma and then my world variable and if i run this i guess i should print it out first if i run this i get hello world again so wait a minute it did the same thing as we had originally it didn't have this space well it didn't have that it didn't have our space down here so the thing with join that's nice is is i can tell it that every time it hits a new word no spaces here means essentially that is just going to combine them together if i want a space here every time i basically if i put a space here then it's going to say every time i hit a new word do a space and then do the next one so if i run this i get hello world now what's nice about this and this will come later and this could be a whole nother video on join is i could actually do hyphen and you'll see that it's going to do every time every time in between it's going to be the hyphen so it's going to go hello hyphen world and so that's kind of thing that's nice with join is you can kind of add some other things like if i even did this say that's our other string that we added in there so you can see it went hello world and then added the hyphen after the end so that's kind of cool with join where you don't have to if you just have these bunch of words that you want to combine together with different whether it's commas whether it's um so some sort of delimiter you could essentially just put that in and it'll combine everything with that way and so if we go back here and then just do a space we end up like we wanted with our hello world so that is the second way that you can add or concatenate strings in python is with the join method or the join operator with strings so the third way i'm going to do is with the actual percent operator so um and some people with math it might be known as the modulus operator but in this instance with strings it's basically just going to be like a string formatter and so to do it i'm essentially going to do start my string i'm gonna do percent s space percent s and then end my string and then i'm gonna do again percent and since i have two words what i'm gonna do is i'm gonna pass in parentheses i'm gonna pass in hello and then world and then if we print this um you should see that we end up with hello world hello world hello world so again we have right here if i make a comment the plus operator we have join and we have percent i'm gonna say i'm gonna say formatter i mean it is an operator but all right so that's three now there's two left so the la the second last one that i want to show you is with um what's called format so essentially what you can do is i can say just to print it out for everybody i can have my string and then i could say this i could say bracket space bracket and then i can do dot format and again format itself i could spend another thing on another video on format and what you can do inside the brackets but essentially to just combine strings you could essentially just do this where it's empty brackets and then you do hello world and now if i print that so this should be our force so we should see four hello worlds here boom hello world right there the last one that you can use is what's called an f string and um f strings are actually only available for python versions 3.6 and above that's when they are introduced and i would say that they are fantastic um essentially what you can do with an f string so a normal string looks like this right you can have that like so i can do like uh single quotes hello world double quotes hello world or i can do the triple quote hello world right well with an f string what it looks like and you can do again these could be the videos themselves in that string you do f and then you just do it you put the quotes and you can do f and this um so essentially you just put an f in front and then what you can do what's really cool is you can put the brackets and you can put the actual variable name hello essentially it's just like format um because you could do this similar thing type of thing with format um but it's less i mean look at the length of line the length of code the amount of code there versus there basically it's like doing the format where you know format we said hey i want this one to go into this one i want this one to go on this one f string basically when python 36 came out they added you the ability where you just say well just put your variables in there it'll do it and so that's one thing that's really cool because now this should be this is not concatenating this is just adding another print another thing to be printed so what you'll see one two three four five now you're saying well zach you just said that this is not to concatenate i'm not concatenating um i'm just printing out concatenating is combining a string to make one now in the in the output of our thing it looks like i did it looks like a concatenated one through five but if um if i were to assign these variables then like if i wanted to print out this whole string by itself that's a whole different thing because then you have to convert an integer into a string or do something like now that would be concatenating and adding the numbers so i'll show the different ways um so if i run that again i should get this exact same output and there it is i do so basically that's the five different ways that you can concatenate strings in python i mean they're very simple um python strings uh when you want to combine them the probably the number one thing that you always use is the plus operator it's the one of the most common one of the things that's always used um the join operator is mostly the join this join function with the strings is mostly when you want to like kind of format stuff in a way that's using like a dilemma a delimiter such as like if you want if you're trying to make a csv file or trying to make a string with a special character between all the words so you can do maybe some start splitting creating lists or something like that the formatter is a lot of times i think people use when they're like oh i have this like maybe it's like a log message and they want to say you know input the variable name so i do something like um either one of these three i guess as usually used with that so i mean concatenation string is really simple it's just taking strings combining together and these are the five ways you can do it so create your own strings test out these different methods see which ones you like the best my personal favorite is either going to be number this plus operator or the f string especially if you have a python version um of 3.6 and above in this version we are using python 3.9 if you follow one of my other tutorials on how to install python you'll see that that's the one that we grabbed so i would just say keep practicing keep playing with it and keep having fun and coding and without further ado have a blessed dress tonight and keep on coding you
Info
Channel: Case Digital
Views: 521
Rating: 5 out of 5
Keywords: Software, python, python tutorial, programming, programmer, python programming, how to concatenate strings in python, concatenate strings in python, python string concatenation, python strings, python tutorial for beginners, how to concatenate strings python, python string tutorial, string concatenation python, string concatenation, how concatenate string in python, how to concatenate two strings in python with space, how to concatenate 2 strings in python
Id: EQMVUy33m-Q
Channel Id: undefined
Length: 11min 30sec (690 seconds)
Published: Mon Jan 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.