0

I would like to extract one element from list in python.

For example:

a = [0,1,2,3]
b = ['0','1','2','3']
a1 = a[0]
b1 = b[0]
print(a1)
print(b1)

As expected, the code print 0 for both a1 and b1.

However I would like to get '0' for both a1 and b1 with single quotation mark instead of 0.

Any idea or help would be really appreciate.

Thank you,

Issac

Isaac
  • 885
  • 2
  • 15
  • 35
  • 1
    Do you want just to print it, or do you need the three-characters-long string '0' for further processing? – GPhilo Nov 03 '17 at 22:51
  • 1
    This is not how values and types work. the output of `print(0)` and `print('0')` will be the same in almost any language, but that's also mostly completely irrelevant. if you want to print a single quote, `print("'")` a single quote. –  Nov 03 '17 at 22:51
  • @GPhilo: I need the three-characters-ling string '0'. Thank you, – Isaac Nov 03 '17 at 22:57
  • 1
    @Isaac you define a string `my_string = "'{}'".format(a1)`. `my_string` will contain `'0'` and have length 3. – GPhilo Nov 03 '17 at 23:01
  • @GPhilo: Thank you!! – Isaac Nov 03 '17 at 23:02

4 Answers4

2

Quotation marks are not parts of a value - they are only for distinguish e. g. the number 1 from the string with the only symbol 1.

The print() function don't print quotation marks even around strings (unlike an interactive session when you give as input only a variable name or an expression).

So you have manually put them into the print() function, e. g.

print("'" + str(a1) + "'")
print("'" + b1 + "'")

or

print("'{}'".format(a1))
print("'{}'".format(b1))

or - in Python 3.6+ -

print(f"'{a1}'")
print(f"'{b1}'")
MarianD
  • 13,096
  • 12
  • 42
  • 54
  • the `print()` function outputs only the value to the screen. the quotes are not part of the (string) value '1'. –  Nov 03 '17 at 22:55
  • @hop I don't understand - how it is related to my answer? – MarianD Nov 03 '17 at 22:58
  • it further clarifies your answer. i didn't want to be rude and edit your answer. –  Nov 03 '17 at 23:00
  • @hop - Oh, I understood your comment now - you are right and I am going to put it in my answer. Thanks! – MarianD Nov 03 '17 at 23:27
1

Normally, Python will print a string without quotes. That's standard in almost all programming languages.

However, Python does let you print a string with the quotes, you just need to tell it to print the representation of the string. One way to do that is with the !r formatting directive.

The items in a are integers, if you want them printed as strings you need to convert them to strings.

a = [0, 1, 2, 3]
b = ['0','1','2','3']
a1 = a[0]
b1 = b[0]

print('{!r}'.format(str(a1)))
print('{!r}'.format(b1))

output

'0'
'0'

You can read about the representation of a string in the official tutorial.

There are other ways to see the representation of a string. For example, you can call the built-in repr function:

print(repr(b1))

Bear in mind that if a string already contains single-quote chars then its representation will be enclosed in double-quotes:

s = "A 'test' string"
print(repr(s))

output

"A 'test' string"

And if the string contains a mixture of quotes then the representation will revert to using single quotes, and using backslash escapes to denote single quotes:

s = "A 'test' \"string\""
print(repr(s))

output

'A \'test\' "string"'

So if for some reason you don't want that behaviour, then you will need to print the quote marks explicitly:

s = "A 'test' string"
print("'{}'".format(s))

output

'A 'test' string'
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • you should not rely on the behaviour of `repr()` in this case. just print the quotes explicitely. –  Nov 03 '17 at 22:58
  • @hop What do you mean, exactly? I think my answer covers most of the common options. – PM 2Ring Nov 03 '17 at 23:07
  • my comment was from befor the edit. the second half of you answer shows why it is a bad idea to rely on `repr()`. –  Nov 04 '17 at 15:09
0

well,

>>> print(repr(str(a1)))
'0'
>>> print(repr(str(b1)))
'0'

will do, but as commented by others I am unsure about your intentions.

progmatico
  • 4,714
  • 1
  • 16
  • 27
0

I searched online and found Why are some Python strings are printed with quotes and some are printed without quotes?

I think if you print as follows, it will work as you wish:

print(repr(a1))
print(repr(b1))
Chun-Yen Wang
  • 568
  • 2
  • 10