Results 1 to 8 of 8

Thread: Python - Adding to the beginning of a new line

  1. #1
    Join Date
    May 2008
    Beans
    1,029

    Python - Adding to the beginning of a new line

    I am trying to add a "- " to the beginning of each new line in a string. Here is my code:
    PHP Code:
    #! /usr/bin/env python
    #
    text "First Line\nSecond Line\nThird Line"
    print text
    print

    text_group text.split("\n")

    for 
    nl in text_group:
        
    spaced "- %s" nl
        
    print spaced

    print
    print 
    spaced 
    The output is:
    Code:
    First Line
    Second Line
    Third Line
    
    - First Line
    - Second Line
    - Third Line
    
    - Third Line
    When I print from inside the "for" loop, each line is printed. But if I print outside the loop, only the last line is printed. How can I get all lines to print outside the loop?

  2. #2
    Join Date
    Nov 2008
    Location
    /home
    Beans
    289
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python - Adding to the beginning of a new line

    Quote Originally Posted by dodle View Post
    I am trying to add a "- " to the beginning of each new line in a string. Here is my code:
    PHP Code:
    #! /usr/bin/env python
    #
    text "First Line\nSecond Line\nThird Line"
    print text
    print

    text_group text.split("\n")

    for 
    nl in text_group:
        
    spaced "- %s" nl
        
    print spaced

    print
    print 
    spaced 
    The output is:
    Code:
    First Line
    Second Line
    Third Line
    
    - First Line
    - Second Line
    - Third Line
    
    - Third Line
    When I print from inside the "for" loop, each line is printed. But if I print outside the loop, only the last line is printed. How can I get all lines to print outside the loop?
    Look at what you are doing with "spaced" in the for loop. When the for loop is done "spaced" will contain the last line.

    Code:
    var = "some string"
    var += " some other string"
    print var
    > "some string some other string"
    Desktop: Windows 7 / Ubuntu 10.04
    Laptop-: Vista
    Netbook: Ubuntu 10.04

  3. #3
    Join Date
    May 2008
    Beans
    1,029

    Re: Python - Adding to the beginning of a new line

    Thanks Sprut1. I've found a solution:
    PHP Code:
    #! /usr/bin/env python
    #
    text "First Line\nSecond Line\nThird Line"
    print text
    print

    new_group = []
    text_group text.split("\n")

    for 
    nl in text_group:
        
    spaced "- %s" nl
        new_group
    .append(spaced)

    joined_group "\n".join(new_group)
    print 
    joined_group 
    Output:
    Code:
    First Line
    Second Line
    Third Line
    
    - First Line
    - Second Line
    - Third Line

  4. #4
    Join Date
    Jun 2007
    Beans
    692

    Re: Python - Adding to the beginning of a new line

    This kind of operation is a good place to use a list comprehension:
    PHP Code:
    text "First Line\nSecond Line\nThird Line"
    joined_group '\n'.join(["- %s" line for line in text.split('\n')]) 

  5. #5
    Join Date
    Nov 2008
    Location
    /home
    Beans
    289
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python - Adding to the beginning of a new line

    Quote Originally Posted by imdano View Post
    This kind of operation is a good place to use a list comprehension:
    PHP Code:
    text "First Line\nSecond Line\nThird Line"
    joined_group '\n'.join(["- %s" line for line in text.split('\n')]) 
    This is exactly why I love Python.
    Desktop: Windows 7 / Ubuntu 10.04
    Laptop-: Vista
    Netbook: Ubuntu 10.04

  6. #6
    Join Date
    May 2008
    Beans
    1,029

    Re: Python - Adding to the beginning of a new line

    Quote Originally Posted by imdano View Post
    This kind of operation is a good place to use a list comprehension:
    PHP Code:
    text "First Line\nSecond Line\nThird Line"
    joined_group '\n'.join(["- %s" line for line in text.split('\n')]) 
    Wow, that's a lot less code. I'll give it a shot. Thanks.

  7. #7
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python - Adding to the beginning of a new line

    Quote Originally Posted by imdano View Post
    This kind of operation is a good place to use a list comprehension:
    PHP Code:
    text "First Line\nSecond Line\nThird Line"
    joined_group '\n'.join(["- %s" line for line in text.split('\n')]) 
    It's not limited to lists, but tuples also:

    PHP Code:
    text "First Line\nSecond Line\nThird Line"
    joined_group '\n'.join(("- %s" line for line in text.split('\n'))) 
    I remember reading that with tuples it uses less memory or something, not sure where I read it.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  8. #8
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Python - Adding to the beginning of a new line

    Code:
    >>> text = "First Line\nSecond Line\nThird Line"
    >>> print "-" + text.replace("\n","\n- ")

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •