Python Forum
How to find the '\n' character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find the '\n' character
#1
I have a text string, which is extracted from a list: 'first_name\nlast_name\nemail_address'

I want to find the position of the '\n' character so I can slice the string and assign each section to its own variable: first_name, last_name, email_address.
I figured that if I could find the position of '\n' I could slice the appropriate section. EXCEPT... what should work:

pattern = r'\'
text.find(pattern) continually returns -1, meaning that it can't find the new line character, even though that sneaky puppy is hiding in plain sight.

What's the best/easiest way to separate these three fields into three separate variables?

Thanks,

Larry
Reply
#2
str.slitlines() or more universal str.split('\n') methods, e.g
first, last, email = 'first_name\nlast_name\nemail_address'.splitlines()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Perfect.

splitlines() seems to work the best.

Thank you!

Larry
Reply
#4
'\n' is shorter than lines, by 1 character!

string = 'Peggy Sue\nWithers Smythe\[email protected]'
string.split('\n') # ['Peggy Sue', 'Withers Smythe', '[email protected]']
Reply
#5
Quote:pattern = r'\'
text.find(pattern) continually returns -1
No it doesn't. That code has an error and doesn't run. Python will not accept a string literal that ends with a single backslash. Maybe you meant this:
pattern = '\\'
text.find(pattern)
That would return -1.
Quote:even though that sneaky puppy is hiding in plain sight.
Plain sight is a poor guide. For example:
x = ["The quotes are an illusion.", bytes("Ignore the leading b.", "utf-8"), "This is a single backslash \\."]
print(x)
print(x[0], x[1].decode("utf-8"), x[2])
Output:
['The quotes are an illusion.', b'Ignore the leading b.', 'This is a single backslash \\.'] The quotes are an illusion. Ignore the leading b. This is a single backslash \.
Well, that's confusing. Printing the same thing two different ways produces two different results.

The confusion is caused by python having 2 different ways to print an object, __str__() and __repr__(). These are "magic" functions defined for many python classes that produce a string appropriate for printing. __str__() is the function called when you call str(x) or the print() function. It is supposed to produce the more attractive output fo the two. __repr__() prints a more informative representation of the object, sometimes adding extra characters for the sake of better understanding. In a str, __repr__() replaces non-printable characters with escape sequences (carriage returns become \n), adds extra backslashes before backslashes in the string so you know they aren't part of an escape sequence, and prints byte strings in their most ascii form, using escape sequences for non-printable characters, and enclosed those in b'' so you know it is a bytes object. When you print a list, the object in the list are printed using __repr__().
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of escape character in re.sub and find WJSwan 1 2,115 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  [solved] unexpected character after line continuation character paul18fr 4 10,609 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 5,076 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 4,314 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Find string and add character - newbi PyDK 1 3,136 May-15-2019, 01:22 PM
Last Post: ichabod801
  Replace changing string including uppercase character with lowercase character silfer 11 10,503 Mar-25-2019, 12:54 PM
Last Post: silfer
  How Do I find Index of a character in string? ilcaa72 5 5,682 May-23-2018, 11:44 PM
Last Post: wavic
  find a character in a string in python3 tony1812 6 9,065 Sep-27-2017, 03:28 PM
Last Post: nilamo
  SyntaxError: unexpected character after line continuation character Saka 2 22,252 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020