How to print only the line which starts with P

0 votes

How to print only the line which starts with P?

Hello = ('P: @haha 279\n G: @hohi 393\nP: @yes 934')
Nov 4, 2020 in Python by Rajiv
• 8,870 points
1,018 views

1 answer to this question.

0 votes

Hi, you might have found another way already- but split the string by the newline delimiter, and again by P using str.startswith().  For more string manipulation methods in the standard Python library refer to:
string — Common string operations — Python 3.9.1rc1 documentation

e.g.

lines = Hello.split("\n")
matches = list(filter(lambda line: line.startswith("P"), lines)
# ALTERNATIVELY: ignore leading whitespace
# matches = list(filter(lambda line: line.lstrip().startswith("P"), lines)
print("\n".join(matches))
answered Dec 2, 2020 by Tyler
• 140 points

Related Questions In Python

0 votes
2 answers

how to print the current time using python?

print(datetime.datetime.today()) READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
1,109 views
0 votes
1 answer

How to handle AssertionError in Python and find out which line or statement it occurred on?

Use the traceback module: import sys import traceback try: ...READ MORE

answered Dec 18, 2018 in Python by charlie_brown
• 7,720 points
5,684 views
0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
2,855 views
0 votes
1 answer

How to output the rows which are affected using SQLAlchemy in Python?

Hi, good question. This is actually not ...READ MORE

answered Feb 15, 2019 in Python by Nymeria
• 3,560 points
3,370 views
0 votes
0 answers

how to print all the roles used by windows instances.

Mar 12, 2019 in Python by Smith
578 views
0 votes
1 answer

how do i print only the last two letters in a string using regular expression in python?

$ to match the end of the ...READ MORE

answered Mar 15, 2019 in Python by Waseem
• 4,540 points
924 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,422 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,042 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP