Thread: Output_string

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    2

    Output_string


    Hello again so I have this code that takes all the punctuation out of the input (this is from a python textbook) but my question is how does the program know what output_string is equal too

    punctuation = "#()*+,-/:<=>?\@^_`{|}~[]"

    print (" ")

    print ("*****")

    print (" ")

    input_string = input (" Enter text string: ")

    output_string = " "

    space_flagged = False


    for char in input_string:
    if char == '.':
    print (output_string)
    output_string = ' '

    elif char not in punctuation:
    output_string += char
    space_flagged = False

    else:
    if not space_flagged:
    output_string += ' '
    space_flagged = True

    print (output_string)

    I have tried just running bits of the code and when I do the program prints output_string as nothing. Also what is a flag?

    Comments on this post

    • Lux Perpetua disagrees : code tags.
    • Winters agrees : Balance.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Book: Discard it.

    flags: Just a variable name. Common meaning is the data is Boolean. True or False. Suppose you run a complicated algorithm and but merely want to know if a pattern was found. You'd "set a flag" or make sure to clear it. Or, to tell the regular expression search that you want to ignore case, you'd "set a flag".

    Code:
    import string
    punctuation = set(string.punctuation)
    
    def remove(Input,Set):
        return [a for a in Input if a not in Set]
    
    ''.join(remove('T;h....i:::s \'"i,s? a pun[ctu][[a)))ted string',punctuation)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    2
    ah that makes sense but how does the program know what output_string is because the value if just empty quotes
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    look through the program you posted line by line. Some places you'll see
    output_string += char
    or
    output_string= ' '

    These statements change output_string
    There's still another few places of
    print(output_string)

    I can't figure out why the book author would want to remove punctuation from a sentence, or why their algorithm happens to be the smartest way to do it.

    On periods the program displays its current collection and a new line.

    Sometimes it sticks in spaces.

    Whatever. I still say "discard your book".
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by b49P23TIvg
    I can't figure out why the book author would want to remove punctuation from a sentence,
    Really? I can think of several reasons Thatís the way you begin when you want to read a text into a corpus of some kind.

    I think the original script was meant the parse the text into words sentence by sentence, and thatís why periods are handled specially. Ugly coding though.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)

IMN logo majestic logo threadwatch logo seochat tools logo