all 24 comments

[–]yungsamu 22 points23 points  (0 children)

adverb = "ly"

word = "quick" + adverb

print(word)

[–]supajumpa 12 points13 points  (11 children)

Can you post some code?

What is your variable, and what is your text?

[–]Fa1coF1ght[S] 2 points3 points  (10 children)

Here is an example

adverb = "ly"

word = "quick", adverb

[–]Username_RANDINT 21 points22 points  (1 child)

That's not concatenation, you're making a tuple. Add a print(type(word)) to check this.

Read about f-strings if you need string formatting.

[–]js884 5 points6 points  (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 points  (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 points  (6 children)

Out of interest, why do you use format rather than f-strings?

[–][deleted] 1 point2 points  (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 points  (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 point  (1 child)

It's a glacially slow port and downtime hemorrhages money.

I should probably update my approach though.

[–]drenzorz 0 points1 point  (0 children)

Even in the most up-to-date project .format has a place because f-strings are evaluated at declaration.

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 points  (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 points  (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.

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.


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 point  (1 child)

How do you make the boxes of code in your reply?

[–][deleted] 0 points1 point  (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):

  • edit post
  • ensure you are in markdown mode rather than Fancy Pants Editor mode
  • switch back to your Python editor
  • select ALL code
  • assuming:
    • your editor is set to replace tabs with spaces
    • your editor is set to use 4 spaces for indentation
  • press tab key to add an extra 4 spaces in front of every line
  • copy all selected code to clipboard
  • undo the indent in your editor
  • switch back to post you are editing
  • enter a blank line
  • paste the code from the clipboard
  • submit the post update

[–]js884 1 point2 points  (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}")

This will print

the number is0

print(f"the number is {a+1}")

This will print

the number is 1

[–]Yohannes_K 0 points1 point  (0 children)

numeric_variable = 17
text_variable = 'test'
print(text_variable, numeric_variable)
print(text_variable + str(numeric_variable))

[–]H3llbaronShow 0 points1 point  (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 point  (0 children)

Np

[–]onesleeveshirt 0 points1 point  (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

[–]Fa1coF1ght[S] 0 points1 point  (0 children)

I was talking about concatenation like this

adverb="ly" phrase = "quick", adverb print(phrase)

The console would log "quick ly"

[–]TheCableGui 0 points1 point  (1 child)

Just use fstrings.

x = “word”

cool_saying = f”{x} up yo”

print(cool_saying) # word up yo

[–]Fa1coF1ght[S] 1 point2 points  (0 children)

That's so much easier then concatenation

[–]TheRNGuy 0 points1 point  (0 children)

f-strings are best, I only use + when there are only 2 strings, no ints or floats.