Open In App

Python | Printing String with double quotes

Last Updated : 19 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Many times, while working with Python strings, we have a problem in which we need to use double quotes in a string and then wish to print it. This kind of problem occurs in many domains like day-day programming and web-development domain. Lets discuss certain ways in which this task can be performed.

 Method #1 : Using backslash (“\”) This is one way to solve this problem. In this, we just employ a backslash before a double quote and it is escaped. 

Python3




# Python3 code to demonstrate working of
# Printing String with double quotes
# Using backslash
 
# initializing string
# using backslash
test_str = "geeks\"for\"geeks"
 
# printing string
print("The string escaped with backslash : " + test_str)


Output : 

The string escaped with backslash : geeks"for"geeks

Time Complexity: O(1)

Auxiliary Space: O(1)

  Method #2 : Using triple quotes This is one more way in python to print and initialize a string. Apart from multiline comment, triple quotes are also good way of escaping. 

Python3




# Python3 code to demonstrate working of
# Printing String with double quotes
# Using triple quotes
 
# initializing string
# using triple quotes
test_str = """geeks"for"geeks"""
 
# printing string
print("The string escaped with triple quotes : " + test_str)


Output : 

The string escaped with triple quotes : geeks"for"geeks

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(1)

Auxiliary Space: O(1)

Approach#3:  Using regular expression

this approach involves using regular expressions to match and replace the backslash and double quote combination in the input string. The resulting string is returned.

Algorithm

1. Import the re module.
2. Define a regular expression pattern to match the backslash and double quote combination.
3. Use the re.sub() function to replace each occurrence of the pattern with just a double quote.
4. Return the resulting string.

Python3




import re
 
def print_string(test_str):
    pattern = r'\\"'
    return re.sub(pattern, '"', test_str)
 
test_str = 'geeks\\"for\\"geeks'
print(print_string(test_str))


Output

geeks"for"geeks

Time Complexity: O(n), where n is the length of the input string, because we iterate over each character in the string once to replace the backslash and double quote combinations using the re.sub() function.

Auxiliary Space: O(n), where n is the length of the input string, because we create a new string to store the resulting string.

Approach#4:  Using reduce method:

Algorithm:

  1. Import the required modules.
  2. Define a function named print_string that takes test_str as input.
  3. Define a regular expression pattern pattern to match the escaped double quotes.
  4. Use the reduce() function to apply a series of substitutions to the input string.
  5. The reduce() function takes a lambda function, a list of tuples containing the pattern and the replacement string, and the input string as arguments.
  6. In each iteration, the lambda function applies the regular expression pattern to the string and replaces the matched pattern with the corresponding replacement string.
  7. The output of each iteration is passed as input to the next iteration.
  8. Finally, the reduced string is returned as output.

Python3




import re
from functools import reduce
 
def print_string(test_str):
    pattern = r'\\"'
    return reduce(lambda s, p: re.sub(p[0], p[1], s), [(pattern, '"')], test_str)
 
test_str = 'geeks\\"for\\"geeks'
print(print_string(test_str))
#This code is contributed by Jyothi pinjala


Output

geeks"for"geeks

Time Complexity:  O(n), where n is the length of the input string. The reduce() function takes O(n) time to apply a series of substitutions to the input string. The re.sub() function takes O(1) time to apply a single substitution.

Space Complexity:  O(n), where n is the length of the input string. The reduce() function creates a temporary string in each iteration, which can take up to O(n) space in total. The re.sub() function also creates a temporary string for each substitution, which can take up to O(n) space in total.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads