Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check for a new line in string in Python 3.x?

How to check for a new line in a string?

Does python3.x have anything similar to java's regular operation where direct if (x=='*\n') would have worked?

like image 497
change Avatar asked Sep 02 '25 18:09

change


1 Answers

If you just want to check if a newline (\n) is present, you can just use Python's in operator to check if it's in a string:

>>> "\n" in "hello\ngoodbye"
True

... or as part of an if statement:

if "\n" in foo:
    print "There's a newline in variable foo"

You don't need to use regular expressions in this case.

like image 180
Mark Longair Avatar answered Sep 04 '25 07:09

Mark Longair