-3

I have this:

print('abcd'+'_',cariable)

which gives me this:

abcd_ 2021_mc.txt

But i need this : abcd_2021_mc.txt i.e. no space between _ and 2.

Can anybody suggest me on this. it would be of a great HELP !! thanks in Advance :)

  • please have a look at python's fstrings. `print(f"abcd{cariable}")` will give you the result you want. – 576i Mar 29 '21 at 11:06
  • 1
    Welcome to Stackoverflow. Try to create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Molitoris Mar 29 '21 at 11:07
  • does this help? https://stackoverflow.com/questions/12453580/how-to-concatenate-items-in-a-list-to-a-single-string – Abhi_J Mar 29 '21 at 11:08
  • Does this answer your question? [How to concatenate items in a list to a single string?](https://stackoverflow.com/questions/12453580/how-to-concatenate-items-in-a-list-to-a-single-string) – Abhi_J Mar 29 '21 at 11:08

7 Answers7

1

You can control the separator used by print using the sep parameter.

>>> print('abcd','2021_mc.txt', sep='')
abcd2021_mc.txt
>>> 

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
0

You can build string using the + operator:

a = '_' + '2021'
Oily
  • 538
  • 3
  • 11
0

If the variable cariable is a string then you can do string concatenation directly and just do print('abcd'+'_'+cariable). That should result in abcd_2021_mc.txt.

0

This gives you what you want:

print('abcd' + '_' + cariable)
Henrik
  • 47
  • 7
0

You can use sep argument for print function.

print("abcd", cariable, sep='_')
Hook
  • 355
  • 1
  • 7
0

I am not sure why you are concatenating 'abcd' and '_' when the results are clearly 'abcd_'.

When you print multiple fields, the default separator is a space. But you can override that. I can only deduce that cariable is '2021_mc.txt', so:

print('abcd', '_', cariable, sep='') # no space between 'abcd', '_', and cariable
#or:
print('abcd_', cariable, sep='')
#or:
print('abcd' + '_', cariable, sep='')

Update

>>> cariable = '2021_mc.txt'
>>> print('abcd', '_', cariable, sep='') # no space between 'abcd', '_', and cariable
abcd_2021_mc.txt
>>> #or:
>>> print('abcd_', cariable, sep='')
abcd_2021_mc.txt
>>> #or:
>>> print('abcd' + '_', cariable, sep='')
abcd_2021_mc.txt
>>>
Booboo
  • 38,656
  • 3
  • 37
  • 60
  • Thanks BooBoo. Option 1st and 3rd worked for me. :) – antriksh mathur Mar 29 '21 at 16:46
  • All three should give the same exact results. See updated answer. And I *thought* that the idea was to print *multiple* values with a print statement without intervening spaces and the solution was to specify `sep=''`. Yet you accepted as the *best* answer one that simply avoids printing multiple values altogether and instead just concatenates everything together. I don't get it. – Booboo Mar 29 '21 at 17:11
  • reason why 2nd option not suitable is because the underscore "_" is not adjoined with the string abcd (modified here) and it cannot be placed along with before concatenate operations. So any way other two worked for me. Thanks. :) – antriksh mathur Mar 31 '21 at 09:18
0

Using f-strings:

print(f"abcd_{cariable}")

If you need to have "abcd" and "_" separated:

s = "abcd"
print(f"{s}_{cariable}")
iasonast
  • 1
  • 2