Merge the lines between triple single quotes and then replace the starting quote with '#'

python add single quotes to string
python triple quote
python print string with double quotes
python triple single quote
python escape quotes in string
python multiline string
python single quote in string
difference between single, double and triple quotes in python
'''
Created on Mar 11, 2017

@author: XXZ
This file is to demonstrate the word frequency counter. This is a very 
important practical

'''

Required Output:

#Created on Mar 11, 2017 @author: XXZ This file is to demonstrate the word frequency counter. This is a very important practical

(I want to count it as a comment)

My Code: import re

START_PATTERN = "'''\n"
END_PATTERN = "'''\n"

with open('counter.py') as file:
    match = False
    newfile = None
for line in file:
    if re.match(START_PATTERN, line):
        match = True
        newfile = open('my_new_file.txt', 'w')
        continue
    elif re.match(END_PATTERN, line):
        match = False
        newfile.close()
        continue
    elif match:
        newfile.write(line)
        newfile.write('\n')

This just writes the last multilines comment in the file. Not all.

You can read the whole file in and apply an re.sub in multiline mode. Afterwards (or before, doesn't mater...) just crop those three ' and add #:

import re
with open('test.py', 'r') as f:
    txt = f.read()
    print('IN:\n', txt)
    txt = re.sub('\n', ' ', txt, flags=re.MULTILINE)
    txt = '#' + txt[3:-3]
    print('\nOUT:\n',txt)


IN:
 '''
Created on Mar 11, 2017

@author: XXZ
This file is to demonstrate the word frequency counter. This is a very 
important practical

'''

OUT:
 # Created on Mar 11, 2017  @author: XXZ This file is to demonstrate the word frequency counter. This is a very  important practical  

How To Format Text in Python 3, But some string values may need to include quotation marks, like when we To create strings that span multiple lines, triple single quotes ''' or triple Escape characters all start with the backslash key ( \ ) combined with double quotes to enclose a string that includes text quoted between double quotes. 116 quotes from Between the Lines (Between the Lines, #1): ‘The act of reading is a partnership. The author builds a house, but the reader makes it a home.’ Home

You can use re.sub to replace one or more new-lines (\n) with a singular space. Then strip the result (of any trailing and leading spaces) and concatenate that onto a '#':

import re
'#' + re.sub('\n+',' ',s).strip()
#'#Created on Mar 11, 2017 @author: XXZ This file is to demonstrate the word frequency counter. This is a very  important practical'

Handy one-liners for SED, than one blank line between lines of text. sed '/^$/d;G' # triple space a file sed '​G lines end with CR/LF sed 's/^M$//' # in bash/tcsh, press Ctrl-V then Ctrl-M sed In method 1, # spaces at the beginning of the line are significant, and trailing append it to the previous line # and replace the "=" with a single space sed -e  If a string contains both triple-single-quotes and triple-double-quotes then you will have to escape one of them, but this is an extremely rare situation. share | improve this answer edited Oct 16 '11 at 8:19

Replace \n with . Here is a code:

a = '''
Created on Mar 11, 2017

@author: XXZ
This file is to demonstrate the word frequency counter. This is a very 
important practical

'''

print("#"+a.replace("\n"," "))

Output:

# Created on Mar 11, 2017  @author: XXZ This file is to demonstrate the word frequency counter. This is a very  important practical  

Chapter 6 – Manipulating Strings, won't work, because Python thinks the string ends after Alice , and the rest ( s One benefit of using double quotes is that the string can have a single quote Any quotes, tabs, or newlines in between the “triple quotes” are considered While the hash character ( # ) marks the beginning of a comment for the rest of the line,  Press CTRL-H to bring up the Find/Replace Dialog. Choose the "Regular expressions" checkbox. To add "start-" to the beginning of each line, type ^ in the "Find what" field, and "start-" in the

Using str.join

Ex:

s = '''
Created on Mar 11, 2017

@author: XXZ
This file is to demonstrate the word frequency counter. This is a very 
important practical

'''
print("# "+ " ".join(i.strip() for i in s.split()) )

Output:

# Created on Mar 11, 2017 @author: XXZ This file is to demonstrate the word frequency counter. This is a very important practical

UNIX Shell Quotes - a simple tutorial, The Grymoire's tutorial on quotes in Unix shells. Using the echo command; Quoting a single character with the (There are 7 spaces between 'a' and 'b'.) It is easier to use pairs of quotation marks to indicate the start and end of the will display each line after the variables and meta-characters have  This post is a long-format reply to Jonathan Jordan's recent post. Jonathan's post was about the non-capturing backreference in Regular Expressions. He and I are both working a lot in Behat, which relies heavily on regular expressions to map human-like sentences to PHP code. One of the common patterns in that space is the quoted-string, which is a fantastic context in which to discuss the

Consider being even more opinionated about quotes · Issue #51 , One thing I've come to appreciate from JS land is that even though ' and " are I also agree that starting a string with " makes it apostrophe-proof smile IMO the only quote transformation that is a clear win is transforming all triple single quotes word to a string only to then also have to change the quotes. Try this - first, get rid of the $ with Find and Replace. Then: Select the column in which the digits are found in text format. Set the cell format in that column as "Number". Choose Edit - Find & Replace ; In the Search for box, enter ^[0-9] In the Replace with box, enter & Check Regular expressions ; Check Current selection only ; Click

Triple Quotes For Multi-Line String in Python, Learn How to use Triple Quotes to Print Multi-Line String in Python From Python Programming perspective, each of these lines is unique contains punctuation marks like a single quote or an apostrophe then Once Python interpreter encounters the opening triplet of the quotation mark, it understands  It would be great if we could switch between single and double quotes. for example 'some string' to "some string" and vise versa. I understand it's may not looks like relevant to the purpose of this extension but most of the times the reason of selecting strings is just changing ' or " quotes.

YAML Multiline Strings, If instead you want them to be replaced by spaces, use the folded style, indicated by The default, clip, puts a single newline at the end of the string. To remove all newlines, strip them by putting a minus sign ( - ) after the style indicator. of spaces you're using to indent a block will be automatically guessed from its first line. Replace double quotes with a single quote within a double quoted string Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is.

Comments
  • What have you tried, and what are you stuck on?
  • Why is the output expecting a split before the word frequency counter?
  • Its not split, its supposed to be all in the same line.
  • Why do you want to do this? Unlike docstrings, comments aren't available to help etc.
  • They aren't just comments. They're docstrings. Maybe adding some real context to the question would help?
  • Thanks it worked. Can you explain the "flags = re.MULTILINE"
  • Normally re works on a line by line base, so you just can't match newlines. This mode makes re handle the complete text as one block. See also docs.python.org/3/library/re.html#re.MULTILINE
  • Correct, even if I'd replace '\n' by '\s', so that two lines do not stick too close together...
  • It doesn't concatenate the lines in one line.
  • @nehaj see the answer now; is it not exactly the same as your desired?
  • it puts the '#' in front of each line. But doesn't concatenate the lines into one.
  • No it doesnt, it gets rid of all lines so by definition the result is only one line