How To Concatenate Strings And Int In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up everybody welcome back to case digital zack here and in this video we're going to answer the question of how to concatenate strings and ants in python so without further ado let's start coding what is up everybody uh in this video like i mentioned we're gonna answer that question of how can i concatenate strings and answer basically how can i concatenate strings and numbers um this is probably one that happens a lot because people learn about string concatenation if you haven't checked out one of my previous videos um when you find out that you can do the plus operating or stuff like that people try it with ants and then they probably run into something an error and so just to kind of show you to kind of show you probably what the error that you're running into is say i have a string that says total i'm going to just do a variable and say this is this is my string right and from string concatenation you learn that i say oh i want to print this total out and i want to add you know five five to it you know it's five bucks whatever so they come and they run a little stream catenation show do this and they come down here get really excited python whoa there's an air and it says only can only concatenate string not into a string so how do we concatenate them well in this video we're going to solve this problem we're going to solve this error using four different methods to go about doing this and the first method that you can use is essentially if you've checked out one of my other videos talking about how to convert an into a string we're gonna we're basically gonna use that information to help us to concatenate strings and ants um and the first way to do it out of four is going to be using the str function so essentially what i can do is before like if i run this again just so everyone refresh we get an error but if i do total plus and then i wrap my five in the str function and i run this now i get total equals five so what's happening there is this str function is essentially when you wrap that around anything it converts it to a string so i'm converting this automatically into a string [Music] using the scr function and it's really simple it's probably the one that you'll probably use most often but there are cases where you may want to do something a little different and another way that you can go about this is you can go about this using the uh the modular or the percent operator and so essentially what we're going to do if i copy this percent s percent five so what is happening is i'm taking my string right and i'm going to convert this five into a string and then i'm going to concatenate it together to get our total equals five dollars so if i run this you should see that it gets the same exact thing as we got above which is what we want right um really simple that's that's another way to go about doing it if for whatever reason the str function doesn't work i haven't run into a lot of cases and i can't think of really any case off the top of my head where that str doesn't work but just in case this is a way that you can go as well the third way that you can do this is actually using the format the format function now you can go about this two different ways so i'll show those here so what you can do is you can do print total plus for our concatenation because we're gonna do a string and i can do this so take my five like one on my previous video say take my five converted your string then add the string to that and that will give us our total down there like normal or what you can do is i can do this and you can see then this is a good i'm i'm glad that i did it this way because you can see that i i basically said the format says take wherever these brackets are take whatever i pass into the format function and put them there and you can see i put total equals you know dollar sign this and i passed in five and so we wanted this but we got total and then we had a dollar sign space and that's because of this extra space here so if i were to just do that this would actually give us what we're looking for um so that is one way another way to go about doing it and just one thing to watch out with with the format is depending on you know how you set this up um could be how you want the string to be to look or it could not be which when i first did it i had it this way and then that is not how we wanted it we wanted it to be dollar sign five with no space in between but doing this gave us something different than we want so we had to go back and change it to that where they're together to give us this value so that is format you could do it that way just watch out with with the spacing because whatever spaces you put here like i could put you know was that four spaces and i could run this and now it's gonna put four spaces in between total and my string of five so that's just something to be careful with but that is something you can do with format it's really powerful we could spend a whole video on it but just something to watch out for and the last but not least probably one of my favorite ways of going about concatenating and converting into strings is using f strings um because what's really cool about f strings granted i should probably put the disclaimer python 3.6 and above um this is when it first got implemented but essentially what you can do is print and i can do f and then give it the string whatever one i want to use i can use single quote i can use double quote um i'm just going to use single and now i can put my variable and this is actually just like i think this is the reason i think f streaming exists it's just a better form it's an it's an updated way to do format so again just like format we have to be careful and i want to put five what it looks like so we just have to be careful what we do is because if i do total and i do space it's going to give us that initial one that we didn't want right but if i do with no space that is going to give us what we want and so those are the four different ways that i have found that you can essentially concatenate strings um and ants together and it's basically just using the way that you convert an into a string and then just come using just general concatenation to go about it so there you have it so that is four different ways that you can go about concatenating ins and strings together so play with them leave a comment below let me know what you like let me know if you found any other ways to do it i'd surely um be glad to hear what the ways different ways that y'all have done it and just practice it play with it see what you find and uh don't forget to subscribe i really appreciate everyone taking the time to view the video um please subscribe to my channel help and let's learn together more about python and software development and without further ado keep on coding
Info
Channel: Case Digital
Views: 974
Rating: 5 out of 5
Keywords: Software, software developer, developer, python, python tutorial, programming, programmer, python programming, how to concatenate strings and int in python, how to combine strings and numbers in python, how to combine strings and ints in python, strings and ints python, how to concatenate int and string in python, how to concatenate string and int in python, how to concatenate strings with int in python, can we concatenate string and integer in python, combine strings and ints python
Id: uIdWbZfQu00
Channel Id: undefined
Length: 8min 18sec (498 seconds)
Published: Wed Jan 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.