DEV Community

Vishal J
Vishal J

Posted on

July 9, 2024 Print() - Part 1

Contents:

  • About print function and its arguments
    • Sep argument
  • Different types of print formats
  • Using escape sequences with print function
  • Rasting - To insert file path
  • Typecasting - To change data format to another format
  • Learnt about how to print multi line string
  • Sting Multiplication
  • Different methods of print formats like F String
  • To see the different methods for a function by using dir command before a function
  • Usages of print function

  • `print("")

    • This is call as print function
  • Different cases with print() function

    • print("Hello", "World") \Seperator
      • Output: Hello World
        • There is a space between two words due to default sep argument
    • print("Hello", "World", sep="-")
      • Output: Hello-World
    • print("Hello", "World", sep="\n") \ Enter or new line
      • Output: Hello World
    • print("Hello", "World", sep="\t") \Tab
      • Output: Hello World

Cancatinating string

  • In Python language, every thing is consider as objects, every object has special functionalities.

  • print("Hello" + " World" + "Welcome" + " To Python Class")

    • Output: Hello WorldWelcome to Python Class
  • print("Hello", "World", end="?"

    • Output: Hello World?

Ecape Sequence

  • For print pattern
    • print("#\n##\n###\n#####")
    • output: # ## ### ####
  • Some Escape sequences
    • \n
    • \t

Rasting

  • To inset print native file path

  • print(r"C:\users/syedajafer/document")

    • Output: C:\users/syedajafer/document
  • Insert double quatation the value will be in string

Type casting

  • print("abc" + str(123))

    • Output: abc123
  • To add Quatations in a string

    • print(' "Hello world"')
      • Output: "Hello world"
    • print(" 'Hello world' ")
      • Output: 'Hello world'

Multi line string

  • To print as paragraph print(""" My Name is Vishal. How are you? What is your name? """)
  • Output: My Name is Vishal. How are you? What is your name?

String Multiplication

  • print("Hello" * 3)
    • Output: HelloHelloHello
  • print("Hello" * 3 *2)
    • Output: HelloHelloHelloHelloHelloHello

Format

`
name = "abc"
age = 25
print("Name: {}, Age:{}".format(name, age))

Output:
Name: abc, Age: 25
`

  • print("Name: {name}, Age: {age}" .format(name="abc", age=25))

Output: Name: abc, Age: 25
`

  • ` print("Name: {name}, Age: {age}" .format(name=["abc"], age=25))

Output: Name: ['abc'], Age: 25
Note: It will also print the strings in list data Structure
`

  • F string ` name = "Syed" age = 26 print(f"Name: {name}, Age: {age}")

Output: Name: Syed, Age: 26
`

  • "Hello World" * 2

    • Output: Hello WorldHello World
    • Use can use printfunction without print function syntax
  • To see available methods for a function use dir

Usages of print() function

  • software development while debugging by checking what is the result at each stages
  • While going to production, they remove print function and insert log key
  • Desktop application to print the status

Top comments (0)