Skip to main content

Printing the text horizontally in Python Programming language

Hi guys,
        We know that the "print" function is used to print the data to the standard output device in Python Programming language. But, the print function automatically adds a newline character every time we cal it.

       For example, consider a program segment to display numbers from 1 to 5:

                                            
                              for n in range(1,6):
                                   
print n
      
   The output of the code will be:

                              1
                              2
                              3
                              4
                              5

  But what if we want the output to be something like:

                1 2 3 4 5

   How do we go about it?


    The answer to this is indeed very simple. The key is to add a "," after the variable
 at the end of the print statement. Consider the following code to print the even and
odd numbers in the given range, to illustrates the technique:


#-*-*-*-*-*-*-*-*-        CODE BEGINS           -*-*-*-*-*-*-*-*-*-*-
limit = int(input("Enter the maximum limit\n"));

print"The Even numbers in the given range are:\n"

for i in range(0,limit,2):
   
print i

print"\nThe odd numbers in the given range are:\n"

for j in range(1,limit,2):
   
print j,


#-*-*-*-*-*-*-*-*-        CODE ENDS           -*-*-*-*-*-*-*-*-*-*-

The output of the above code is:


Enter the maximum limit
10
The Even numbers in the given range are:

0
2
4
6
8

The odd numbers in the given range are:

1 3 5 7 9


So as we can see, the "," added after j helps us to print the odd numbers in the
horizontal fashion.

And also, please not that the range() function can take two or three parameters.
Hence the range() function is polymorphic in nature.
I'll be posting more about the range() and xrange() functions in another post of mine.


I hope you like this post. Please do leave your valuable comments below.

    Thank you very much.


Comments

Popular posts from this blog

Kotlin :A New Beginning

Hello guys, You might have heard about a new language supported for the android Officially by Google,Named as Kotlin. Kotlin is a statically typed Programming language.By statically typed language i mean that the type checking is done during complile time. It was developed in July 2011 by Jetbrains in Russia,In 2017 Google declared Kotlin as the official langauge for android. However the Traditional Java developers are sticking to java. New programmers who want to make android apps,Web apps can use Kotlin as a language of preference. With this Post i welcome you to the world of Kotlin. There is no good book for kotlin but there is the official documentation on Kotlin.I refer the official Kotlin documentation. The main aim of this post if to Introduce you Kotlin and its Uses and also how to write programs in Kotlin. The official documentation can be found on the website   KOTLIN I belive that this post will help you to write codes in Kotlin. So lets begin Fir...

Introduction to Web

Hello everybody,I would like to begin this topic by giving you some idea about "what is web development?" First of all,Let me tell you in simple words that "Web" and "Internet" are not the same.   Internet is the collection of computer's connected together in a network. It connects millions of computers together globally, forming a network in which any computer can communicate with any other computer as long as they are both connected to the  Internet . The World Wide Web (abbreviated  WWW  or the Web)  is  an information space where documents and other web resources are identified by Uniform Resource Locators (URLs), interlinked by hypertext links, and can be accessed via the Internet. Its very easy to learn website development and its very simple. Lets talk about the technologies used to develop a website and also lets get started with a few. There are a lot of languages to develop websites.In order to master all these languages what yo...

Web programming part 2

Hello guys, I am back with another post on web programming. In the last post you guys had learnt how to create a simple HTML document. As i told you there are many tags that you might have to remember. In this Post i will be creating a small Login form using only HTML. By using only HTML i mean No styling will be given to the document. The purpose of this code is to demonstrate you tags like <form> and <label> you will also learn how to make a button in HTML. The code goes like this: < form  action ="/action_page.php" >      < label > < b > Username < /b > < /label >      < input  type ="text"  placeholder ="Enter Username" >      < label > < b > Password < /b > < /label >      < input  type ="password"  placeholder ="Enter Password" >      < button  type ="submit" ...