Formatting string and adding to line

This is the place for queries that don't fit in any of the other categories.

Formatting string and adding to line

Postby Dariusz » Tue Oct 01, 2013 9:04 pm

Heya

I have qTextEdit with text

testa
testb
testc

I want it to look like this

testa;testb;testc;

I got so far with this
Code: Select all
 fromQT = self.qe.toPlainText()
        toPlain=str(fromQT)
        prefix = ''.join((';',toPlain))
        mystr = ''.join([line.strip() for line in prefix])


I cant figure out how to add to each line rather than to all...

Thanks, bye.
Dariusz
 
Posts: 6
Joined: Wed Aug 07, 2013 1:21 pm

Re: Formatting string and adding to line

Postby micseydel » Tue Oct 01, 2013 9:53 pm

How about
Code: Select all
yourStr.replace('\n', ';')

?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1202
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Formatting string and adding to line

Postby Dariusz » Tue Oct 01, 2013 10:03 pm

Ok that works but uhh how?! I tried so long and nothing worked :/

Is the '/n' stand for next line? Where can I read more about those things?
Dariusz
 
Posts: 6
Joined: Wed Aug 07, 2013 1:21 pm

Re: Formatting string and adding to line

Postby micseydel » Tue Oct 01, 2013 10:24 pm

\n (and not /n) is how you put a literal newline in strings (other than multi-line strings). Any basic tutorial should mention it. If yours didn't, you should read a different one.

Here's an alternative way to do it, assuming you have an iterable of the words (you can get it with the string and .splitlines() or .split('\n'))
Code: Select all
>>> words = ['testa', 'testb', 'testc']
>>> ';'.join(words)
'testa;testb;testc'

For what you have here, the .replace() method is probably better though.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1202
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Formatting string and adding to line

Postby Dariusz » Wed Oct 02, 2013 8:30 am

Heya
Oh I read quite few tutorials. They all were quite basic tho :( Is there any particular tutorial u could recommend maybe?
Thanks for help.
Dariusz
 
Posts: 6
Joined: Wed Aug 07, 2013 1:21 pm

Re: Formatting string and adding to line

Postby dboxall123 » Wed Oct 02, 2013 10:26 am

dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Formatting string and adding to line

Postby pythonsunu » Wed Oct 02, 2013 11:03 am

Thanks the tutorials are great!!!
pythonsunu
 
Posts: 4
Joined: Wed Oct 02, 2013 10:24 am

Re: Formatting string and adding to line

Postby Dariusz » Wed Oct 02, 2013 9:03 pm

That resource is just... mind blowing! What would I give when I 1st started coding in python 1 year ago! Absolutly love it. Just the introduction already answered bunch of my questions. The biggest thank you for this I ever could give to any one ! Wow thankzzzzzzzzzzzzzzzzzzzzzzzz. I'm gonna dive in to it straight away :)
Dariusz
 
Posts: 6
Joined: Wed Aug 07, 2013 1:21 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests