Page 238 - Ai_V1.0_Class9
P. 238

Syntax to use the print( ) function is:

              print(object(s), sep = separator, end = end)
              Where,
                 • object can be one or more separated by comma and it can be a variable, literal, expression. An object will be
                 converted to string before printed.
                 • sep parameter specifies the separator between the items you want to print. By default, the separator is a space.
                 You can change this to any string you want.
                 • end parameter allows you to specify what to print at the end of the output. By default, end is set to a newline
                 character ('\n'), which means each print statement will end with a new line.

              Following are some of the examples of the print( ) function:

                                            Commands                                          Output
                    print("hello")                                                hello

                    print("hello", "friends")                                     hello friends
                    print("hello" + "friends")                                    hellofriends

                    (This is concatenation of string using "+"
                    operator)
                    print("hello", "friends", sep="$")                            hello$friends
                    print("hello", "friends", sep="$", end="!")                   hello$friends!
                    print("hello", "friends", end="!")                            hello friends!

                    print("hello")                                                hello
                    print()

                    print("friends")                                              friends
                    print("hello", end="%")                                       hello%friends

                    print("friends")
                    print("hello", "friends", "welcome", sep="#",                 hello#friends#welcome@
                    end="@")
                    print("I am in class", 9)                                     I am in class 9
                    a = 5
                    b = 10
                    print("Sum of two numbers is " + str(a + b))
                                                                                  Sum of two numbers is 15
                    age = 14

                    print("I am ", age, " years old")
                                                                                  I am 14 years old
              NOTE:  sep and end are always the last two parameters after the objects in the print statements. However both
                     may have interchange positions.
                   print(10,20 ,sep="+",end="!")
              OR

                   print(10,20, end="!",sep="+")
              Output in both the cases will be same as both the statements are interpreted as same.


                    236     Artificial Intelligence Play (Ver 1.0)-IX
   233   234   235   236   237   238   239   240   241   242   243