Page 458 - ComputerScience_Class_11
P. 458

Example:
                    Program.py

                 File  Edit  Format   Run   Options   Window    Help

                 f_name="Anjali"
                 l_name="Singh"
                                                                                  Output
                 # with end default value
                 print("Hello", f_name , l_name)                               Hello Anjali Singh
                 print("Hello", f_name , l_name, sep="****")                   Hello****Anjali****Singh



              3.  end:
                 Defines what is printed at the end of the output. By default, print() adds a newline (\n) at the end, but you can
                 change this as per your requirement.
                Example:

                   Program.py
                File  Edit  Format   Run    Options   Window    Help

                f_name="Anjali"
                l_name="Singh"
                # with end default value
                print("Hello", f_name , l_name)
                                                                            Output
                print("I Love Python.")
                # with end different value                               Hello Anjali Singh
                print("Hello", f_name , l_name, end="****")              I Love Python.
                print("I Love Python.")                                  Hello Anjali Singh****I Love Python.


              Python provides f-strings (formatted string literals) that allow you to display text and variable values together in a
              simple way. By placing f before the quotation marks and writing variables inside { }, Python automatically replaces
              them with their values when the program runs.
              Syntax:

              print(f"text {variable_name}")
              where,
              •  print(): This function displays the output on the screen.
              •  f before the quotation marks: It tells Python that the string is a formatted string (f-string).
              •  "text": This is the normal message you want to show.
              •   {variable_name}: The variable is written inside curly braces. Python replaces it with the value stored in that
                 variable when the program runs.
              Example:

                   Program.py
               File  Edit  Format    Run   Options   Window    Help

                name = "Pranya"
                marks = 99

                print(f"{name} scored {marks} marks in the test.")





                  456  Touchpad Computer Science (Ver. 3.0)-XI
   453   454   455   456   457   458   459   460   461   462   463