%s or %d the string replacement or formatters are not work in my python 3.6

my name is bhargav and from last so many days i try to do %s or %d as study in the codecademy. it work online fine but if i type same code in my pc installed python (3.6) is show me error every time. it indicate me the % symbol side error. ex

a = ‘bhargav’
b = ‘dhaval’

print (“my name is %d and my brother name is %d.”,%(a,b))

i tried so many ways and trouble shoot problem but same happened in all even it also happened if i write code in ‘PYCHARM’ ide that also show me the same error.

%d is for decimal numbers, i would use %s instead given you are dealing with strings.



you have ,, when using % you don’t need a ,, the comma is for joining strings together. % is used to fill the place holders

i remove the coma ‘,’ but still nothing happened. i tried all this things before.

this is the code i ran:

a = 'bhargav'
b = 'dhaval'
print ("my name is %s and my brother name is %s." %(a,b))

and it works fine? how can it not work for you?

ya bro i tell same thing if i try this code on online it works fine but if i try same code in my installed python 3.6 than it show me error. which version you are use ???

did you run my code just to be sure?

i ran this in 3.5.2, so not that much behind. You could upgrade to .format() which is recommended to use instead since the start of python3:

a = 'bhargav'
b = 'dhaval'
print ("my name is {} and my brother name is {}.".format(a,b))

ya bro this work perfect but why the %d or %s not work ??? do you have any idea ???

can you confirm (since i can’t see what you are doing) the code above doesn’t work? I know you said you tried many times, but can you explicitly confirm the code above doesn’t work?

well %d obviously doesn’t work, since it is for decimal numbers.

it seems they haven’t released 3.6 for linux, so i can’t run it yet.

3.6 hasn’t been released, full stop.

Building it isn’t so bad though:

$ hg clone https://hg.python.org/cpython
$ cd cpython
$ ./configure && make
<wait a few minutes while it builds>
$ ./python
Python 3.7.0a0 (default:897fe8fa14b5, Oct 14 2016, 18:43:26)
[GCC 6.2.1 20160830] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'bhargav'
>>> b = 'dhaval'
>>> print ("my name is %s and my brother name is %s." %(a,b))
my name is bhargav and my brother name is dhaval.

Might want to sneak this in before configuring:

$ hg checkout v3.6.0b2

As seen above, that code does execute, and they wouldn’t be breaking backwards-compatibility like that anyhow, what runs in 3.5 also runs in 3.6. @bhargavna01 you simply didn’t run it correctly.

1 Like

bro just check this three files. or tell me if anywhere i am wrong or make mistake. first file is from pycharm and other two are from python which are installed in my system.
And thanks for the solution.Uploading… Uploading… Uploading…

bro ur usnig linux os and your python is 3.7.0 so may be b in your case it works but in my pc it does not. wait i will send you my screen shot .

look at this my screen shot. and now you tell me where i can make mistake. but same code if i run on the codecademy than it works fine. Uploading…

Contrary to what you said, you have not removed that comma. You’ve probably removed it on codecademy though, otherwise that would also not run because that comma isn’t allowed there, it doesn’t match python’s syntax

Side note: Remember that Python will highlight the first thing it does not expect, not the error itself. That would be just before the highlighted code. As others have pointed out, the comma is just before the error indicator.

1 Like

yes you are right thanks man for helping me.