Jump to content

Concatenating Strings in Python

Go to solution Solved by Blade of Grass,

You will use string concatenation all the time to build your output messages.

 

I've dug through some of the work I did awhile ago (and yay! VB6):

Msg = "There are" & SquareMeter & " square meters in" & Acres & " acres." Msg = "Your order total for" & SquareFootage & " square feet of flooring is " & Format$(Total, "currency") Msg = "The total commission on your sale of " & Format$(FinalPrice, "currency") & " is " & Format$(Cost, "currency") & "."
String concatenation (the symbol VB6 uses is the &) is very useful, above are three different examples of it being used (it was from review work I had to do).

Hello,

 

I am abit confused what you would use concantenating strings for in Python... Here is the code.

print ("You can concantenate two  " + "strings with the '+' operator.")

Thanks to anyone that can help,

 

- Harry

It seems impossible until it's done.

Link to comment
https://linustechtips.com/topic/308634-concatenating-strings-in-python/
Share on other sites

Link to post
Share on other sites

e.g.

what's your name : Bill computer responds: "your name is "Bill" "

 

Depends on what you're really doing, it could be used for an all manner of things. 

 

Or maybe a program that gets the date and you convert it to a string, so "Todays date is" + whatever variable for the date. 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to post
Share on other sites

e.g.

what's your name : Bill computer responds: "your name is "Bill" "

 

Depends on what you're really doing, it could be used for an all manner of things. 

Thanks mate

 

Seems pretty pointless to be though :P

It seems impossible until it's done.

Link to post
Share on other sites

Thanks mate

 

Seems pretty pointless to be though :P

I found a list of practical applications on wikipedia, but sure there's more, personally I just think of it as an easy way to nicely format data that's been inputed or calculated from another value you've inputted perhaps.  the applications on wikipedia are mostly audio feeback systems

 

"This technique is also used in number change announcements, voice mail systems, or most telephony applications that provide dynamic feedback to the caller (e.g. moviefonetellme, and others)."

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to post
Share on other sites

Say you are making a program which requires you to log in you need it to display the username at some point which is stored in a variable. With string concatenation you don't usually just concatenate two bare strings, it's usually done with a string (or a few) and a variable (or a few), e.g:

print("Username:\n"+usrname)

"My game vs my brains, who gets more fatal errors?" ~ Camper125Lv, GMC Jam #15

Link to post
Share on other sites

Say you are making a program which requires you to log in you need it to display the username at some point which is stored in a variable. With string concatenation you don't usually just concatenate two bare strings, it's usually done with a string (or a few) and a variable (or a few), e.g:

print("Username:\n"+usrname)

I don't mean using variables...

 

The code which I used in the OP prints like this:

 

You can concentrate two strings with the '+' operator.

 

:)

It seems impossible until it's done.

Link to post
Share on other sites

I don't mean using variables...

 

The code which I used in the OP prints like this:

 

You can concentrate two strings with the '+' operator.

 

:)

Well yeah that is pointless if you're just doing:

print("part of the string"+" other part of the string")

"My game vs my brains, who gets more fatal errors?" ~ Camper125Lv, GMC Jam #15

Link to post
Share on other sites

Come on guys, at least try to stay on topic.

 

As for the OP, concatenating two string literals (things enclosed in quotes) is fairly useless, although if your lines are getting too long, it can be good to break them up into multiple shorter lines, and concatenating string literals is a way to split them up. Once you involve variables though, it becomes far more useful, allowing you to do stuff like ("hello "+name).

HTTP/2 203

Link to post
Share on other sites

Come on guys, at least try to stay on topic.

 

As for the OP, concatenating two string literals (things enclosed in quotes) is fairly useless, although if your lines are getting too long, it can be good to break them up into multiple shorter lines, and concatenating string literals is a way to split them up. Once you involve variables though, it becomes far more useful, allowing you to do stuff like ("hello "+name).

Thanks :)

It seems impossible until it's done.

Link to post
Share on other sites

Well, I did a small project a while back where you had to describe different adjectives of an object (i cant remember what it was) and I used a for loop and if statment to keep adding different attributes to the sentence.

 

EX: string = "The door is"

 

if (color == red)

string = string + "red"

 

etc....

 

This is just a super basic example, but trust me, it is not pointless.

Link to post
Share on other sites

You will use string concatenation all the time to build your output messages.

 

I've dug through some of the work I did awhile ago (and yay! VB6):

Msg = "There are" & SquareMeter & " square meters in" & Acres & " acres." Msg = "Your order total for" & SquareFootage & " square feet of flooring is " & Format$(Total, "currency") Msg = "The total commission on your sale of " & Format$(FinalPrice, "currency") & " is " & Format$(Cost, "currency") & "."
String concatenation (the symbol VB6 uses is the &) is very useful, above are three different examples of it being used (it was from review work I had to do).

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

You will use string concatenation all the time to build your output messages.

 

I've dug through some of the work I did awhile ago (and yay! VB6):

Msg = "There are" & SquareMeter & " square meters in" & Acres & " acres." Msg = "Your order total for" & SquareFootage & " square feet of flooring is " & Format$(Total, "currency") Msg = "The total commission on your sale of " & Format$(FinalPrice, "currency") & " is " & Format$(Cost, "currency") & "."
String concatenation (the symbol VB6 uses is the &) is very useful, above are three different examples of it being used (it was from review work I had to do).

 

Thanks, this helped alot :D

It seems impossible until it's done.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×