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,880 points
62 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
194 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 17, 2018 in Python by charlie_brown
• 7,780 points
3,101 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,130 points
262 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,520 points
1,451 views
0 votes
0 answers

how to print all the roles used by windows instances.

Mar 12, 2019 in Python by Smith
76 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
247 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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,033 views
0 votes
0 answers
+4 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,810 points
1,557 views