Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107049 > unrolled thread

How to print a part of a string?

Started bydurgadevi1 <srirajarajeswaridevikrupa@gmail.com>
First post2016-04-15 05:13 -0700
Last post2016-04-15 12:40 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  How to print a part of a string? durgadevi1 <srirajarajeswaridevikrupa@gmail.com> - 2016-04-15 05:13 -0700
    Re: How to print a part of a string? Joel Goldstick <joel.goldstick@gmail.com> - 2016-04-15 08:28 -0400
    RE: How to print a part of a string? Dan Strohl <D.Strohl@F5.com> - 2016-04-15 14:27 +0000
    Re: How to print a part of a string? durgadevi1 <srirajarajeswaridevikrupa@gmail.com> - 2016-04-16 00:25 -0700
    RE: How to print a part of a string? Joaquin Alzola <Joaquin.Alzola@lebara.com> - 2016-04-15 12:40 +0000

#107049 — How to print a part of a string?

Fromdurgadevi1 <srirajarajeswaridevikrupa@gmail.com>
Date2016-04-15 05:13 -0700
SubjectHow to print a part of a string?
Message-ID<edfe7d06-e8b1-4cef-92c3-2d1a217ebdac@googlegroups.com>
Hello all,

I have another homework problem.

I have a textfile. It contains lines of string.

I am required to only print out a certain part of the string.

For example the textfile contain:

ABC XXXXX NNNNN
BCD QQQQQ EEEEE
ABC WWWWW AAAAA


I need to print all the parts that come after only ABC.

That means I need to print only XXXXX from the first line and WWWWW from the second line. 

I'm not sure of what code to use to achieve that.

[toc] | [next] | [standalone]


#107053

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2016-04-15 08:28 -0400
Message-ID<mailman.21.1460723321.6324.python-list@python.org>
In reply to#107049
On Fri, Apr 15, 2016 at 8:13 AM, durgadevi1
<srirajarajeswaridevikrupa@gmail.com> wrote:
> Hello all,
>
> I have another homework problem.
>
> I have a textfile. It contains lines of string.
>
> I am required to only print out a certain part of the string.
>
> For example the textfile contain:
>
> ABC XXXXX NNNNN
> BCD QQQQQ EEEEE
> ABC WWWWW AAAAA
>
>
> I need to print all the parts that come after only ABC.
>
> That means I need to print only XXXXX from the first line and WWWWW from the second line.
>
> I'm not sure of what code to use to achieve that.
> --
> https://mail.python.org/mailman/listinfo/python-list

do you know how to open the file and do a for loop to get each line in
the string?

If you do, do you know about this:

>>> s = "ABC XXXXX NNNNN"
>>> s_split = s.split()
>>> s_split
['ABC', 'XXXXX', 'NNNNN']
>>> s_split[0]
'ABC'
>>>



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays

[toc] | [prev] | [next] | [standalone]


#107058

FromDan Strohl <D.Strohl@F5.com>
Date2016-04-15 14:27 +0000
Message-ID<mailman.26.1460730463.6324.python-list@python.org>
In reply to#107049
As with lots of things in python, there are lots of ways of approaching this, here are some hints for you to think about (in no particular order):

- REGEX 
- replace()
- string[:y]
- split()

And of course, you could consider creating a table with every possible string that could start with "ABC", and it's matching non-"ABC" string, then lookup the string in the table and get your match, it's a bit brute forcey, but it would work (eventually) <grin>

Good luck in your homework!

Dan




> -----Original Message-----
> From: Python-list [mailto:python-list-bounces+d.strohl=f5.com@python.org]
> On Behalf Of durgadevi1
> Sent: Friday, April 15, 2016 5:13 AM
> To: python-list@python.org
> Subject: How to print a part of a string?
> 
> Hello all,
> 
> I have another homework problem.
> 
> I have a textfile. It contains lines of string.
> 
> I am required to only print out a certain part of the string.
> 
> For example the textfile contain:
> 
> ABC XXXXX NNNNN
> BCD QQQQQ EEEEE
> ABC WWWWW AAAAA
> 
> 
> I need to print all the parts that come after only ABC.
> 
> That means I need to print only XXXXX from the first line and WWWWW from
> the second line.
> 
> I'm not sure of what code to use to achieve that.
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#107085

Fromdurgadevi1 <srirajarajeswaridevikrupa@gmail.com>
Date2016-04-16 00:25 -0700
Message-ID<e495af3e-ec0c-44ac-985c-9ca00353d608@googlegroups.com>
In reply to#107049
On Friday, April 15, 2016 at 8:13:17 PM UTC+8, durgadevi1 wrote:
> Hello all,
> 
> I have another homework problem.
> 
> I have a textfile. It contains lines of string.
> 
> I am required to only print out a certain part of the string.
> 
> For example the textfile contain:
> 
> ABC XXXXX NNNNN
> BCD QQQQQ EEEEE
> ABC WWWWW AAAAA
> 
> 
> I need to print all the parts that come after only ABC.
> 
> That means I need to print only XXXXX from the first line and WWWWW from the second line. 
> 
> I'm not sure of what code to use to achieve that.

Yay! I'm able to complete my homework now. :) Thanks for your replies guys :)

[toc] | [prev] | [next] | [standalone]


#107090

FromJoaquin Alzola <Joaquin.Alzola@lebara.com>
Date2016-04-15 12:40 +0000
Message-ID<mailman.44.1460797936.6324.python-list@python.org>
In reply to#107049
list_append = []

with open('filename') as f:
for line in f:
t_line = line.split(' ')
if (t_line[0] == 'ABC'):
print(t_line[1])
list_append.append(t_line[1])


-----Original Message-----
From: Python-list [mailto:python-list-bounces+joaquin.alzola=lebara.com@python.org] On Behalf Of durgadevi1
Sent: 15 April 2016 13:13
To: python-list@python.org
Subject: How to print a part of a string?

Hello all,

I have another homework problem.

I have a textfile. It contains lines of string.

I am required to only print out a certain part of the string.

For example the textfile contain:

ABC XXXXX NNNNN
BCD QQQQQ EEEEE
ABC WWWWW AAAAA


I need to print all the parts that come after only ABC.

That means I need to print only XXXXX from the first line and WWWWW from the second line.

I'm not sure of what code to use to achieve that.
--
https://mail.python.org/mailman/listinfo/python-list
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web