-4

How can I convert time_1 into time_2?

>>> time_1 = '2019-01-01T00:00:00-06:00'
>>> time_2 = ???
>>> print(time_2)
"'2019-01-01T00:00:00-06:00'"

I have tried the following, but it gives the wrong output:

>>> time_1 = '2019-01-01T00:00:00-06:00'
>>> time_2 = "'" + time_1 + "'"
>>> print(time_2)
'2019-01-01T00:00:00-06:00'

Which is how time_1 looks like, not how time_2 should look like.

I know that when I go to print time_2, its output looks like time_1, but that's the point of this question - I want the printed output to look like time_2.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
carsof
  • 83
  • 1
  • 8
  • 3
    You know that the outer quote pair, in each case, is not part of the actual string value, yes? – jarmod Aug 05 '21 at 17:43
  • use ```\'``` or ```\"``` – NeonFire Aug 05 '21 at 17:43
  • 1
    How would you add *any* character to the end of a string? – Scott Hunter Aug 05 '21 at 17:44
  • you aren't putting *double quotes around `time_q`*, you are putting *single quotes around `time_1` to get to `time_2`*. I.e. ``time_2 = f"'{time_1}'"`` – juanpa.arrivillaga Aug 05 '21 at 17:45
  • "But, it gives this output: `'2019-01-01T00:00:00-06:00'`". So, exactly the same as what yours gives. – Kelly Bundy Aug 05 '21 at 17:57
  • Exactly ^ -- it doesn't give the output I'm looking for – carsof Aug 05 '21 at 17:58
  • [Exact same output](https://tio.run/##K6gsycjPM7YoKPr/vyQzNzXe0FZB3cjA0FLXwBCIQgwMrMBI18AMSKpzgdUY2Soo4VGkxFVQlJlXogFRq8nFRbzBCiCTlRS0FSBagAwldOP@/wcA). – Kelly Bundy Aug 05 '21 at 17:59
  • I'm confused? I was agreeing with you-- it outputs the same thing as time_1. But, I want it to output time_2 – carsof Aug 05 '21 at 18:00
  • It *is* the same as your time_2, *not* the same as your time_1. – Kelly Bundy Aug 05 '21 at 18:01
  • "But, it gives this output:" Yes; *that is the correct output*. Just like how when you `print(time_1)`, it shows you `2019-01-01T00:00:00-06:00`, and **not** `'2019-01-01T00:00:00-06:00'`, and that, too is the correct output. You must make sure you understand the difference between objects in themselves, and representations of those outputs. – Karl Knechtel Aug 05 '21 at 18:02
  • Does this answer your question? [Why are some Python strings are printed with quotes and some are printed without quotes?](https://stackoverflow.com/questions/17171889/why-are-some-python-strings-are-printed-with-quotes-and-some-are-printed-without) – mkrieger1 Aug 05 '21 at 18:02
  • I guess what I'm trying to ask in this thread is that I want the printed output to LOOK like time_2-- I know if I were to print time_2 it'd give me just the single quotes. But, I want it to look like time_2 when it's printed – carsof Aug 05 '21 at 18:03
  • If you want it to look like that when printed you need to add single quotes *and* double quotes. – mkrieger1 Aug 05 '21 at 18:04
  • Right-- and that's where I'm not sure how to do that. I guess I need to read up on what single and double quotes mean because apparently I have the wrong definition – carsof Aug 05 '21 at 18:06
  • No, you have the right definition of single and double quotes: single quotes are `'` and double quotes are `"`. It's just that in your code you added *only* single quotes, and now you need to add double quotes on top of that. `'"' + string_with_single_quotes + '"'`. To represent either of the two quotes in Python, you have to quote it, too. – mkrieger1 Aug 05 '21 at 18:08
  • Okay I just combined your answer + someone else's answer and figured out the solution. Thanks – carsof Aug 05 '21 at 18:12
  • @KellyBundy hopefully my update clarifies why I said the output was not equal to time_2 – carsof Aug 05 '21 at 18:15

2 Answers2

2

Your title says "How to put double quotes around a string with single quotes?"

However, your example suggests that you want to put single quotes around a string.

If you just wanted to wrap a string with single quotes then do so as if they were any other normal string.

time_2 = "'" + time_1 + "'"

A Kareem
  • 586
  • 3
  • 10
  • 1
    Or more succinctly, `f"'{time_1}'"`. – juanpa.arrivillaga Aug 05 '21 at 17:46
  • " = double quotes right? So I wanted to put " around ' (double quotes around single quotes) – carsof Aug 05 '21 at 17:48
  • @juanpa.arrivillaga `repr(time_1)` is a further char shorter. – Kelly Bundy Aug 05 '21 at 17:49
  • @KellyBundy yeaaaa but I just wouldn't trust that. You are relying on implementation details of `repr`. At least, if you could find some guarantee in the documentation, then maybe – juanpa.arrivillaga Aug 05 '21 at 17:49
  • I just tried what you recommended but the output for time_2 ='2019-01-01T00:00:00-06:00' , which only has single quotes (missing the double quotes) – carsof Aug 05 '21 at 17:51
  • @carsof the `time_2` you provided **doesn't have double quotes**. This is *very important and basic to understand about strings* – juanpa.arrivillaga Aug 05 '21 at 17:52
  • Okay, didn't realize that " is not a double quote. But, again, the solution you posted doesn't convert the answer to how time_2 looks – carsof Aug 05 '21 at 17:54
  • Just updated the answer to show the output of the solution provided- not what I'm looking for – carsof Aug 05 '21 at 17:56
  • "So I wanted to put " around ' (double quotes around single quotes)" **there aren't single quotes in your original string to begin with**. "Okay, didn't realize that " is not a double quote. " It **is** a double quote. The quote marks that you see on either end of a string **in your code** are **not part of the string**. Just like how if you use a `\` character to escape something, that backslash is not part of your string. – Karl Knechtel Aug 05 '21 at 18:03
  • Yes, I understand I have the wrong terminology when it comes to double/single quotes- my bad. – carsof Aug 05 '21 at 18:04
0

I don't think anyone here understood what I was trying to do, but I combined a few of the suggested solutions together and got the output I was looking for:

time_1= '2019-01-01T00:00:00-06:00'
time_2 = '"' + "'"+ time_1 + "'"+ '"'
print(time_2)

Now, the output is:

"'2019-01-01T00:00:00-06:00'"

Which is what I wanted.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
carsof
  • 83
  • 1
  • 8