-4

I have the the following text

text = SCISSOR LIFT 18-19' ELECTRIC 60" LENGTH

I want:

text = SCISSOR LIFT 18-19 FT ELECTRIC 60 INCH LENGTH

I tried text = "SCISSOR LIFT 18-19' ELECTRIC 60" LENGTH" text.replace('"', " ")

But I got

 File "<ipython-input-1-104923c7a47e>", line 1
    text = "SCISSOR LIFT 18-19' ELECTRIC 60" LENGTH"
                                         ^
SyntaxError: invalid syntax

I want to replace the single quotation marks with "FT" and the double quotation marks with inch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Noor Dean
  • 21
  • 2
  • You forgot "I tried..." – chepner Nov 30 '20 at 17:51
  • I'm not sure I understand the question. The topic and what you have written do not convey the same idea. What you _have_ versus what you _want_ seem to indicate you may need to familiarize yourself with Python's notions of strings and how to designate them in your code. Nevertheless, what is presented doesn't ask how it is you want to replace them. Can you help? – Andrew Falanga Nov 30 '20 at 17:52

1 Answers1

0

If you want to work with text, you usually read it from file, process it and write to another file (or screen). In this case, there will be no syntax errors related to special characters (like quotes) in the text.

If you want to put your text in the source file itself, as a literal, you have to be careful with special characters. One method to define a string with both single and double quotes is by using triple quotes:

text = '''SCISSOR LIFT 18-19' ELECTRIC 60" LENGTH'''

Here is a python environment session where I checked that it works:

>>> text = '''SCISSOR LIFT 18-19' ELECTRIC 60" LENGTH'''
>>> print(text)
SCISSOR LIFT 18-19' ELECTRIC 60" LENGTH
>>>

If you manage to do this, you are on right track to do your replace-task. Just give correct parameters to your replace function.

anatolyg
  • 26,506
  • 9
  • 60
  • 134