Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8133055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:29:47+00:00 2026-06-06T09:29:47+00:00

Just for fun, I wrote this simple function to reverse a string in Python:

  • 0

Just for fun, I wrote this simple function to reverse a string in Python:

def reverseString(s):
    ret = ""
    for c in s:
        ret = c + ret
    return ret

Now, if I pass in the following two strings, I get interesting results.

print reverseString("Pla\net")
print reverseString("Plan\et")

The output of this is

te
alP
te\nalP

My question is: Why does the special character \n get translated into a new line when passed into the function, but not when the function parses it together by reversing n\? Also, how could I stop the function from parsing \n and instead return n\?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:29:49+00:00Added an answer on June 6, 2026 at 9:29 am

    You should take a look at the individual character sequences to see what happens:

    >>> list("Pla\net")
    ['P', 'l', 'a', '\n', 'e', 't']
    >>> list("Plan\et")
    ['P', 'l', 'a', 'n', '\\', 'e', 't']
    

    So as you can see, \n is a single character while \e are two characters as it is not a valid escape sequence.

    To prevent this from happening, escape the backslash itself, or use raw strings:

    >>> list("Pla\\net")
    ['P', 'l', 'a', '\\', 'n', 'e', 't']
    >>> list(r"Pla\net")
    ['P', 'l', 'a', '\\', 'n', 'e', 't']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just for fun, I've been using python and gstreamer to create simple Linux audio
I'm writing some code (just for fun so far) in Python that will store
This is going to be a long post and just for fun, so if
Sometimes I want to build Python or GCC from scratch just for fun, but
After answering this question I put together the following C# code just for fun:
I'm making a simple fractal viewing app for Android, just for fun. I'm also
Just for fun, how close can we get to debug an application in C#
Just for fun I created a project that created about 5 GB of memory
I built (just for fun) 3 classes to help me log some events in
I'm learning about AI and (just for fun and practice, not profit or anything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.