Python Programs for Printing Names

Python Programs for Printing Names

In this blog, we will explore some simple Python programs that cover a variety of tasks, including basic input/output, file handling, error handling, and graphical displays using libraries. Let's dive into each program!

 

Agenda

  1. Write a Python program to input your name and age and display them.
  2. Write a Python program to print hello message and ask for my name and find the length of the name.
  3. Write a Python program to create a binary file with name and roll number.
  4. Write a Python program to display your name using interactive mode.
  5. Write a Python program to print your name, class, and roll number.
  6. Write a Python program to write roll number, name, and marks of a student in a data file marks.dat.
  7. Write a Python program to input your name and display them.
  8. Write a Python program to handle NameError.
  9. Write a Python program to print your name 5 times.
  10. Write a Python program to print your name 10 times.
  11. Write a Python program to draw your name using Turtle.
  12. Write a Python program to fill in a letter template given below with name and date.

 

 

Python Programs for Printing Names



1. Input Name and Age, and Display Them

# Program to input name and age, and display them
name = input("Enter your name: ")
age = input("Enter your age: ")
print(f"Your name is {name} and you are {age} years old.")

This program takes the user's name and age as input and displays them back. It uses the input() function to capture user input and print() to output the data.

 

 

2. Print Hello Message, Ask for Name, and Find the Length of the Name

# Program to greet user, ask for name and find the length of the name
print("Hello!")
name = input("What is your name? ")
print(f"Your name is {name} and it has {len(name)} characters.")

The program greets the user, asks for their name, and calculates the length of the name using the len() function. It then prints the name and its character count.

 

 

3. Create a Binary File with Name and Roll Number

# Program to write name and roll number to a binary file
import pickle

name = input("Enter your name: ")
roll_number = input("Enter your roll number: ")

with open("student.dat", "wb") as file:
    pickle.dump((name, roll_number), file)

print("Data has been written to the binary file.")

This program writes the user’s name and roll number to a binary file using Python’s pickle module. The data is stored in a binary format for efficient storage and retrieval.

 

 

4. Display Your Name Using Interactive Mode

# Program to display your name using interactive mode
print("Your name is John Doe")  # Replace with your name

This simple program displays your name on the screen. It demonstrates how to print output directly using Python’s built-in print() function.

 

 

5. Print Your Name, Class, and Roll Number

# Program to print your name, class, and roll number
name = "John Doe"
class_name = "10th Grade"
roll_number = "A123"

print(f"Name: {name}")
print(f"Class: {class_name}")
print(f"Roll Number: {roll_number}")

This program prints your name, class, and roll number. The data is stored in variables and displayed using formatted strings.

 

 

6. Write Roll Number, Name, and Marks of a Student to a Data File

# Program to write roll number, name, and marks to a data file
roll_number = input("Enter your roll number: ")
name = input("Enter your name: ")
marks = input("Enter your marks: ")

with open("marks.dat", "w") as file:
    file.write(f"Roll Number: {roll_number}\n")
    file.write(f"Name: {name}\n")
    file.write(f"Marks: {marks}\n")

print("Data has been written to marks.dat file.")

This program collects roll number, name, and marks from the user and writes them to a file called marks.dat. It uses basic file handling in Python to store the data.

 

 

7. Input Your Name and Display It

# Program to input and display your name
name = input("Enter your name: ")
print(f"Your name is {name}")

The program takes your name as input and displays it. It demonstrates how to interact with users and output results

 

8. Handle NameError Exception

# Program to handle NameError
try:
    print(x)
except NameError:
    print("Variable 'x' is not defined.")

This program raises a NameError intentionally and catches the error using a try-except block. It demonstrates how to handle exceptions gracefully in Python.

 

 

9. Print Your Name 5 Times

# Program to print your name 5 times
for _ in range(5):
    print("Your name is John Doe")  # Replace with your name

This program uses a for loop to print your name five times. It shows how loops work in Python to repeat an action multiple times.

 

 

10. Print Your Name 10 Times

# Program to print your name 10 times
for _ in range(10):
    print("Your name is John Doe")  # Replace with your name

Similar to the previous program, this one prints your name 10 times. The for loop is useful for repetitive tasks in Python.

 

 

11. Draw Your Name Using Turtle

# Program to draw your name using turtle
import turtle

t = turtle.Turtle()
t.write("John Doe", font=("Arial", 16, "normal"))  # Replace with your name

turtle.done()

This program uses the turtle graphics library to draw your name on the screen. It introduces users to graphical programming in Python.

 

 

12. Fill in a Letter Template with Name and Date

# Program to fill in a letter template with name and date
name = input("Enter your name: ")
date = input("Enter the date: ")

letter_template = f"""
Dear {name},

This letter is to inform you that your application has been received.
We will process it and get back to you shortly.

Date: {date}

Best regards,
Company XYZ
"""

print(letter_template)

This program fills in a letter template with the user’s name and date. It uses formatted strings to insert user input into the letter template and prints the result.

 

 

Conclusion

These Python programs cover a wide range of fundamental tasks, from simple input/output operations to file handling and error management. They serve as useful building blocks for more advanced Python projects, and mastering them will give you a strong foundation in programming.


Post a Comment

0 Comments