Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,156,274 members, 7,829,612 topics. Date: Thursday, 16 May 2024 at 09:34 AM

I Need Your Explanation Here Pls - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / I Need Your Explanation Here Pls (1054 Views)

An Advice Needed Here Pls Can I Get Job As A Graduate With Second Class Lower / IT Gurus Here Pls Help Me Answer This Question / Need Urgent Help From Kind Hearted Programmers Here Pls (2) (3) (4)

(1) (Reply) (Go Down)

I Need Your Explanation Here Pls by manuel4real(m): 5:46pm On May 26, 2020
Good day nairalanders. I started learning Python programming language on solo learns a few days ago. While learning I came across this and my question is:

1. Why will someone need to add \n to his program? For instance, print('come\\nhere') gives come\nhere. What's the need for the \n there? Of what importance is It?

1 Like

Re: I Need Your Explanation Here Pls by incogni2o: 5:52pm On May 26, 2020
\n represents a [i]carriage return [/i]also callled a new line.

Its to make the computer know thier is a new line between the two characters

1 Like

Re: I Need Your Explanation Here Pls by LSarcastic: 6:11pm On May 26, 2020
manuel4real:
Good day nairalanders. I started learning Python programming language on solo learns a few days ago. While learning I came across this and my question is:

1. Why will someone need to add \n to his program? For instance, print('come\\nhere') gives come\nhere. What's the need for the \n there? Of what importance is It?

"\n" (backslash n) is basically a newline character. Meaning whenever the program encounters it in a string, the rest of the string is moved to the next line. So, if I run:
print("Hello\nworld"wink

The output will be:
Hello
world

On the other hand, there are cases where you'd like to display "\n" on the screen. Using
print("Hello\nworld"wink
will interpret the character as a newline and you don't want that. The only option is to use another backslash to "escape" it. That means doing
print("Hello\\nworld"wink
instead.

That's why you'll see double backlash in some cases.

Nairaland's parser automatically converts right parentheses to an emoji.
Re: I Need Your Explanation Here Pls by Coder2Client(m): 6:18pm On May 26, 2020
manuel4real:
Good day nairalanders. I started learning Python programming language on solo learns a few days ago. While learning I came across this and my question is:

1. Why will someone need to add \n to his program? For instance, print('come\\nhere') gives come\nhere. What's the need for the \n there? Of what importance is It?
We use \n for next line. If example, you want name, address, number in separate lines you use \n to achieve that.

public class NextLineDemo{
public static void main(String [] args){
System.out.println("Your Name\nAddress\nNumber"wink;

Re: I Need Your Explanation Here Pls by stanliwise(m): 8:47am On May 27, 2020
incogni2o:
\n represents a [i]carriage return [/i]also callled a new line.

Its to make the computer know thier is a new line between the two characters
Basically it means python should end that line and when next python would print it would start on a new line. In It also generates a carriage return characteristics even though it typical don’t mean carriage return.

Carriage return is \r

Incase you wanna know more, google escape characters.

The following is a list of escape sequences.

\n Newline
\t Horizontal Tab
\v Vertical Tab
\b Backspace
\r Carriage Return
\f Form feed
\a Audible Alert (bell)
\\ Backslash
\? Question mark
\' Single quote
\" Double quote
\000 Oct - No one uses Octal unless they have an ICL background...
\xhh Hex number

2 Likes

Re: I Need Your Explanation Here Pls by stanliwise(m): 8:48am On May 27, 2020
LSarcastic:


"\n" (backslash n) is basically a carriage return character. Meaning whenever the program encounters it in a string, the rest of the string is moved to the next line. So, if I run:
print("Hello\nworld"wink

The output will be:
Hello
world

On the other hand, there are cases where you'd like to display "\n" on the screen. Using
print("Hello\nworld"wink
will interpret the character as a newline and you don't want that. The only option is to use another backslash to "escape" it. That means doing
print("Hello\\nworld"wink
instead.

That's why you'll see double backlash in some cases.

Nairaland's parser automatically converts right parentheses to an emoji.
it’s a new line character not necessarily carriage return although it kinda do the two.

1 Like

Re: I Need Your Explanation Here Pls by LSarcastic: 8:24pm On May 31, 2020
stanliwise:
it’s a new line character not necessarily carriage return although it kinda do the two.
You're right. I'll edit the answer to portray it. Thanks

1 Like

(1) (Reply)

Which Cloud Iaas Company Do You Use, And Why? / Tech Support ( Cyber Security Professionals, Let's Meet Here / Altschools Vs ALX

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.