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

17
  • 3
    You know that the outer quote pair, in each case, is not part of the actual string value, yes? Commented Aug 5, 2021 at 17:43
  • use \' or \" Commented Aug 5, 2021 at 17:43
  • 1
    How would you add any character to the end of a string? Commented Aug 5, 2021 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}'" Commented Aug 5, 2021 at 17:45
  • "But, it gives this output: '2019-01-01T00:00:00-06:00'". So, exactly the same as what yours gives. Commented Aug 5, 2021 at 17:57

2 Answers 2

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 + "'"

Sign up to request clarification or add additional context in comments.

10 Comments

Or more succinctly, f"'{time_1}'".
" = double quotes right? So I wanted to put " around ' (double quotes around single quotes)
@juanpa.arrivillaga repr(time_1) is a further char shorter.
@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
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)
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.