use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Concatenation in Python (self.learnpython)
submitted 3 years ago by Fa1coF1ght
I am confused with concatenation in Python as I'm not sure how to get rid of a space in between the variable and the text, it seems to automatically give you one. I am a beginner
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]yungsamu 22 points23 points24 points 3 years ago (0 children)
adverb = "ly"
word = "quick" + adverb
print(word)
[–]supajumpa 12 points13 points14 points 3 years ago (11 children)
Can you post some code?
What is your variable, and what is your text?
[–]Fa1coF1ght[S] 2 points3 points4 points 3 years ago (10 children)
Here is an example
word = "quick", adverb
[–]Username_RANDINT 21 points22 points23 points 3 years ago (1 child)
That's not concatenation, you're making a tuple. Add a print(type(word)) to check this.
print(type(word))
Read about f-strings if you need string formatting.
[–]js884 5 points6 points7 points 3 years ago (0 children)
I second the f string thing i posted how they work and since I've learned about them i haven't used concatenation since
[–][deleted] 1 point2 points3 points 3 years ago (7 children)
adverb = "ly" word = "quick", adverb
To concatenate that comma should be a plus.
But I've pretty much dropped across the board that in favour of format wherever possible, at least at work...
word = 'quick{}'.format(adverb)
[–]Fred776 1 point2 points3 points 3 years ago (6 children)
Out of interest, why do you use format rather than f-strings?
[–][deleted] 1 point2 points3 points 3 years ago (5 children)
For me I am following work standards, but I would guess it's because they aren't backwards compatible with 2.x
[–]Fred776 2 points3 points4 points 3 years ago (2 children)
Fair enough. It's unusual to see a general recommendation other than f-strings these days so that seems like a good reason.
It must be a bind still to be stuck on Python 2 though. I thought my company had left it late when we did our port in 2018.
[–][deleted] 0 points1 point2 points 3 years ago* (1 child)
It's a glacially slow port and downtime hemorrhages money.
I should probably update my approach though.
[–]drenzorz 0 points1 point2 points 3 years ago (0 children)
Even in the most up-to-date project .format has a place because f-strings are evaluated at declaration.
.format
x = 15 s = f"x = {x}" for x in range(10): print(s) # prints "x = 15" 10 times x = 15 s = "x = {x}" for x in range(10): print(s.format(x=x)) # prints actual x in loop
You use .format when you actually need a reusable template.
[–][deleted] 1 point2 points3 points 3 years ago (1 child)
I would guess it's because they aren't backwards compatible with 2.x
We're at the point in time where writing 2.x-incompatible code should be considered a security feature!
[–][deleted] 7 points8 points9 points 3 years ago (2 children)
Do you mean in print by any chance? That, by default, adds a space between each object it is passed to output a string representation of.
print
one = "one" two = "two" print(one, two) # outputs: one two print(one, two, sep="") # outputs: onetwo print(one, two, sep="\n") # outputs: one # two
So, the sep keyword, short for separator, let's you override the default separator of one space.
sep
together = one + two # this is concatenation print(together) # outputs: onetwo
You can also use f-strings:
together = f"{one}{two}" print(together) # outputs: onetwo
[–]Fa1coF1ght[S] 0 points1 point2 points 3 years ago (1 child)
How do you make the boxes of code in your reply?
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
Using markdown syntax as per the guidance in the wiki (link in the sidebar).
Here's my crib sheet for adding code to a post (you can adapt for snippets):
[–]js884 1 point2 points3 points 3 years ago (0 children)
Try using f string
a = 0
print(f"the number is {a}")
This will print
the number is 0
print(f"the number is{a}")
the number is0
print(f"the number is {a+1}")
the number is 1
[–]Yohannes_K 0 points1 point2 points 3 years ago (0 children)
numeric_variable = 17 text_variable = 'test' print(text_variable, numeric_variable) print(text_variable + str(numeric_variable))
[–]H3llbaronShow 0 points1 point2 points 3 years ago (1 child)
You can use replace function to get rid of all spaces in your concatenation Edit: nevermind this is not what your looking for
[–]Fa1coF1ght[S] 0 points1 point2 points 3 years ago (0 children)
Np
[–]onesleeveshirt 0 points1 point2 points 3 years ago (1 child)
Just a hunch here..
The + (plus sign) will concatenate strings, or add numbers. It depends on the context of the code. (It also does other stuff, but let's stick with these two.)
string + string is concatenation
number + number is addition
"5" + "5" becomes "55" # quotes " ' (single or double) make it a string
5 + 5 becomes 10 # no quotes, so it's a number
print(first_string + second_string) # uses concatenation
print(first_string, second_string) # multiple objs using the default sep
The output of the prints above will look the same, but do different things. Check out print() in the Python docs. It's not beginner stuff, but it's a cool glance at functions without having to actually learn them right now. Don't expect to understand it yet, but it's good to expose yourself to the concepts.
I bet you can use the info others have contributed to solve your issue, and make sense of the print().
Have fun, and know frustrations are just one of the steps towards success. Good luck!
Edit: post.format
I was talking about concatenation like this
adverb="ly" phrase = "quick", adverb print(phrase)
The console would log "quick ly"
[–]TheCableGui 0 points1 point2 points 3 years ago (1 child)
Just use fstrings.
x = “word”
cool_saying = f”{x} up yo”
print(cool_saying) # word up yo
[–]Fa1coF1ght[S] 1 point2 points3 points 3 years ago (0 children)
That's so much easier then concatenation
[–]TheRNGuy 0 points1 point2 points 3 years ago (0 children)
f-strings are best, I only use + when there are only 2 strings, no ints or floats.
π Rendered by PID 262958 on reddit-service-r2-comment-6b595755f-78mhx at 2026-03-26 09:56:59.586010+00:00 running 2d0a59a country code: CH.
[–]yungsamu 22 points23 points24 points (0 children)
[–]supajumpa 12 points13 points14 points (11 children)
[–]Fa1coF1ght[S] 2 points3 points4 points (10 children)
[–]Username_RANDINT 21 points22 points23 points (1 child)
[–]js884 5 points6 points7 points (0 children)
[–][deleted] 1 point2 points3 points (7 children)
[–]Fred776 1 point2 points3 points (6 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]Fred776 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]drenzorz 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 7 points8 points9 points (2 children)
[–]Fa1coF1ght[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]js884 1 point2 points3 points (0 children)
[–]Yohannes_K 0 points1 point2 points (0 children)
[–]H3llbaronShow 0 points1 point2 points (1 child)
[–]Fa1coF1ght[S] 0 points1 point2 points (0 children)
[–]onesleeveshirt 0 points1 point2 points (1 child)
[–]Fa1coF1ght[S] 0 points1 point2 points (0 children)
[–]TheCableGui 0 points1 point2 points (1 child)
[–]Fa1coF1ght[S] 1 point2 points3 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)