Python Module 3 SIQ
Python Module 3 SIQ
format() Method:
The format() method helps format strings by inserting values into
placeholders within a string template.
Example :
name = "Alice"
age = 30
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string)
# Output:
My name is Alice and I am 30 years old.
Example:
split() divides a string into a list based on a specified separator, while join()
concatenates elements of a list into a string using a separator.
Example:
text = "apple,banana,orange"
result = text.split(',')
print(result)
# Output: apple,banana,orange
Example:
find() Method:
The find() method returns the index of the first occurrence of a substring
within the string. If the substring is not found, it returns -1.
Example:
# Output: 6
Python's string handling methods are versatile and cater to various string
manipulation needs in tasks like data processing, text formatting, pattern
matching, and more. Understanding and leveraging these methods can significantly
enhance string manipulation capabilities in Python programs.
2. List out all the useful string methods which are supported in python.
Explain with an example for each method.
Answer:
upper() Method:
2. lower() Method:
○ Example:
text = "Hello World" result = text.lower()
print(result) # Output: hello world
3. strip() Method:
○ Example:
text = " Hello World " result = text.strip()
print(result) # Output: Hello World
4. split() Method:
○ Example:
text = "apple,banana,orange" result = text.split(',')
print(result) # Output: ['apple', 'banana', 'orange']
5. join() Method:
○ Description: Joins elements of a list into a string using a specified
separator.
○ Example:
fruits = ['apple', 'banana', 'orange'] result =
','.join(fruits) print(result) # Output:
apple,banana,orange
6. find() Method:
○ Example:
text = "Hello World" index = text.find('World')
print(index) # Output: 6
Use Cases:
● upper() and lower() methods are handy for case manipulation in text
processing.
● strip() method is useful for sanitizing user inputs by removing
unnecessary whitespace.
● split() and join() methods aid in splitting and joining strings based on
specific patterns or delimiters.
● find() method assists in searching and locating substrings within strings.
These string methods play pivotal roles in data cleaning, formatting, searching, and
text manipulation tasks in Python programs, providing flexibility and efficiency in
handling strings.
○ os module:
■ The os module provides a wide range of operating
system-related functionalities, such as interacting with the file
system, managing processes, and handling environment variables.
■ It includes methods for file and directory operations, working
with processes, permissions, and more.
○ os.path module:
■ The os.path module, on the other hand, focuses specifically on
path manipulation-related functions. It deals with operations
related to paths, such as joining paths, splitting paths, checking
existence, and file name manipulations.
■ It doesn’t offer file I/O or specific file/directory manipulation
methods but concentrates on path-related functionalities.
a) chdir() Method:
b) rmdir() Method:
○ Description: Removes a directory.
○ Example:
import os os.rmdir('/path/to/directory')
c) walk() Method:
d) listdir() Method:
○ Use: Retrieves the list of files and directories in the specified path.
e) getcwd() Method:
○ Description: Returns the current working directory.
○ Example:
import os cwd = os.getcwd() print(cwd)
Use Cases:
4.What are the key properties of a file? Explain in detail the file
reading/writing process with an example of a python program.
Answer:
Key Properties of a File:
Python provides built-in functions and methods for reading from and writing
to files. The process typically involves the following steps:
1. Opening a File:
To read from or write to a file, you first need to open it using the open()
function. It requires the file path and the mode ('r' for reading, 'w' for
writing, 'a' for appending, and more).
Always close the file using the close() method to release system
resources.
Use Cases:
Best Practices: