Practical_2: String and files#


Exercise 1:#

Create a string variable text and assign it the value "Python is amazing!".

# Exercise 1

Exercise 2:#

Find the length of the string "Hello, World!" and store it in a variable length.

# Exercise 2

Exercise 3:#

Convert the string "python programming" to uppercase and store it in a variable upper_text.

# Exercise 3

Exercise 4:#

Extract the first 5 characters from the string "Artificial Intelligence".

# Exercise 4

Exercise 5:#

Concatenate the strings "Hello" and "World" with a space in between.

# Exercise 5

Exercise 6:#

Replace the word "dog" with "cat" in the string "The quick brown dog jumps over the lazy dog.".

# Exercise 6

Exercise 7:#

Split the string "apple,banana,orange" by commas and store the result in a list fruits_list.

# Exercise 7

Exercise 8:#

Use string formatting to create the sentence "My name is Alice and I am 25 years old.", where the name and age are variables.

# Exercise 8

Exercise 9:#

Check if the string "python" is present in the sentence "I love python programming".

# Exercise 9

Exercise 10:#

Count the number of occurrences of the letter "e" in the string "Elephant is an elegant animal".

# Exercise 10

Exercise 11:#

Remove any leading and trailing spaces from the string "   Data Science   ".

# Exercise 11

Exercise 12:#

Convert the string "100" into an integer and store it in a variable int_value.

# Exercise 12

Exercise 13:#

Extract the substring "pro" from the string "programming".

# Exercise 13

Exercise 14:#

Check if the string "data" starts with "da" and ends with "ta".

# Exercise 14

Exercise 15:#

Reverse the string "machine learning" and store the result in reversed_text.

# Exercise 15

Exercise 16:#

Convert the first letter of each word in the string "machine learning is fun" to uppercase.

# Exercise 16

Exercise 17:#

Create a multiline string representing a poem and store it in a variable poem.

# Exercise 17

Exercise 18:#

Write a string to a file named "output.txt".

# Exercise 18

Exercise 19:#

Read the contents of a file named "data.txt" and print it line by line.

# Exercise 19

Exercise 20:#

Check if a file "example.txt" exists before opening it.

# Exercise 20

Exercise 21:#

Append a new line "This is a new line" to an existing file "notes.txt".

# Exercise 21

Exercise 22:#

Read the contents of a file "log.txt" and store each line in a list log_lines.

# Exercise 22

Exercise 23:#

Create a new file "report.txt" and write the string "Data analysis report" to it.

# Exercise 23

Exercise 24:#

Replace the word "error" with "issue" in a file "log.txt".

# Exercise 24

Exercise 25:#

Count the number of words in a text file "document.txt".

# Exercise 25

Exercise 26:#

Write a Python script that reads a file "essay.txt" and prints the total number of lines, words, and characters.

# Exercise 26

Exercise 27:#

Copy the contents of one file "source.txt" to another file "destination.txt".

# Exercise 27

Exercise 28:#

Write a Python function that takes a filename as input and returns the number of lines in the file.

# Exercise 28

Exercise 29:#

Extract all the words from a text file "article.txt" that contain more than 5 characters.

# Exercise 29

Exercise 30:#

Convert the contents of a file "notes.txt" to uppercase and save the result in another file "uppercase_notes.txt".

# Exercise 30

Exercise 31:#

Write a Python script that reads a file and removes all empty lines.

# Exercise 31

Exercise 32:#

Open a file "config.txt" in both read and write mode.

# Exercise 32

Exercise 33:#

Write a Python function that reads a file and prints the longest line in the file.

# Exercise 33

Exercise 34:#

Write a Python script to count the frequency of each letter in a text file "message.txt".

# Exercise 34

Exercise 35:#

Write a Python script that reads a file and prints the total number of characters, excluding spaces.

# Exercise 35

Exercise 36:#

Create a Python script to check whether a file is empty or not.

# Exercise 36

Exercise 37:#

Write a Python function that reads a file and removes all punctuation marks from the text.

# Exercise 37

Exercise 38:#

Create a Python function that writes a list of strings to a file, each string on a new line.

# Exercise 38

Exercise 39:#

Write a Python script to count the number of lines that start with a specific letter (e.g., “A”) in a text file.

# Exercise 39

Exercise 40:#

Write a Python function that takes a string as input and writes it to a file "output.txt" in reverse order.

# Exercise 40

Exercise 41:#

Write a Python script that reads a file and prints all the lines that contain the word "Python".

# Exercise 41

Exercise 42:#

Write a Python script that opens a file and removes all numeric digits from the file’s content.

# Exercise 42

Exercise 43:#

Write a Python script to find the most frequent word in a text file "book.txt".

# Exercise 43

Exercise 44:#

Write a Python script that reads a file and prints all the lines that contain exactly 5 words.

# Exercise 44

Exercise 45:#

Write a Python script to read a file and print the first and last line of the file.

# Exercise 45

Exercise 46:#

Write a Python script that checks if a file "students.txt" is writable.

# Exercise 46

Exercise 47:#

Create a Python function that opens a file and returns the number of unique words in the file.

# Exercise 47

Exercise 48:#

Write a Python script that reads a file and prints all lines in reverse order.

# Exercise 48

Exercise 49:#

Write a Python function that reads a file and returns the most common letter in the text.

# Exercise 49

Exercise 50:#

Write a Python script that reads a file and replaces every occurrence of "Python" with "Java".

# Exercise 50