PDA

View Full Version : Python - Adding to the beginning of a new line



dodle
March 30th, 2009, 08:14 AM
I am trying to add a "- " to the beginning of each new line in a string. Here is my 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 spacedThe output is:
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?

Sprut1
March 30th, 2009, 08:32 AM
I am trying to add a "- " to the beginning of each new line in a string. Here is my 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 spacedThe output is:
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.


var = "some string"
var += " some other string"
print var
> "some string some other string"

dodle
March 30th, 2009, 08:44 AM
Thanks Sprut1. I've found a solution:
#! /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_groupOutput:
First Line
Second Line
Third Line

- First Line
- Second Line
- Third Line

imdano
March 30th, 2009, 06:34 PM
This kind of operation is a good place to use a list comprehension:


text = "First Line\nSecond Line\nThird Line"
joined_group = '\n'.join(["- %s" % line for line in text.split('\n')])

Sprut1
March 30th, 2009, 06:56 PM
This kind of operation is a good place to use a list comprehension:


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.

dodle
March 30th, 2009, 10:10 PM
This kind of operation is a good place to use a list comprehension:


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.

Can+~
March 30th, 2009, 10:18 PM
This kind of operation is a good place to use a list comprehension:


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:


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.

ghostdog74
March 31st, 2009, 01:53 AM
>>> text = "First Line\nSecond Line\nThird Line"
>>> print "-" + text.replace("\n","\n- ")