0% found this document useful (0 votes)
1 views4 pages

VTU Python Module 3 Detailed Answers Part2

The document provides an introduction to Python programming, focusing on string methods and file paths. It explains various string methods such as lower(), upper(), and replace(), along with whitespace removal and alignment methods. Additionally, it discusses absolute and relative file paths, highlighting their importance in file management within Python.

Uploaded by

charanbhat5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

VTU Python Module 3 Detailed Answers Part2

The document provides an introduction to Python programming, focusing on string methods and file paths. It explains various string methods such as lower(), upper(), and replace(), along with whitespace removal and alignment methods. Additionally, it discusses absolute and relative file paths, highlighting their importance in file management within Python.

Uploaded by

charanbhat5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

VTU - Introduction to Python Programming - Module 1

CO Mapped: CO1 - Demonstrate proficiency in handling loops and the creation of functions

Q6. List any six methods associated with string and explain each of them with example.

Here are six common string methods in Python with explanation and examples:

1. **lower()** - Converts all characters to lowercase.


Example:
text = 'PYTHON'
print(text.lower())
Output: python

2. **upper()** - Converts all characters to uppercase.


Example:
text = 'python'
print(text.upper())
Output: PYTHON

3. **title()** - Converts the first character of each word to uppercase.


Example:
text = 'python programming'
print(text.title())
Output: Python Programming

4. **replace()** - Replaces a substring with another.


Example:
text = 'I love Python'
print(text.replace('Python', 'Java'))
Output: I love Java

5. **find()** - Finds the first occurrence index of a substring.


Example:
text = 'programming'
print(text.find('g'))
Output: 3

6. **count()** - Counts the number of times a value appears.


Example:
text = 'banana'
print(text.count('a'))
Output: 3
VTU - Introduction to Python Programming - Module 1

CO Mapped: CO1 - Demonstrate proficiency in handling loops and the creation of functions

Q7. Explain the various string methods for the following operations with examples. (i) Removing whitespace

i) **Whitespace Removal Methods:**


- **strip()**: Removes whitespace from both sides.
Example:
text = ' Python '
print(text.strip())
Output: Python

- **lstrip()**: Removes whitespace from the left.


Example:
text = ' Python'
print(text.lstrip())
Output: Python

- **rstrip()**: Removes whitespace from the right.


Example:
text = 'Python '
print(text.rstrip())
Output: Python

ii) **Alignment Methods:**


- **rjust(width, fillchar)**: Right-aligns string.
Example:
print('Python'.rjust(10, '*'))
Output: ****Python

- **ljust(width, fillchar)**: Left-aligns string.


Example:
print('Python'.ljust(10, '-'))
Output: Python----

- **center(width, fillchar)**: Centers the string.


Example:
print('Python'.center(12, '='))
Output: ===Python===

Q8. Write the output of the following: i. 'Hello'.upper().islower() ii. 'Hello'.upper().lower() iii. '-'.join('There can

i. `'Hello'.upper().islower()`
- 'Hello'.upper() -> 'HELLO'
VTU - Introduction to Python Programming - Module 1

CO Mapped: CO1 - Demonstrate proficiency in handling loops and the creation of functions

- 'HELLO'.islower() -> False


**Output:** False

ii. `'Hello'.upper().lower()`
- 'Hello'.upper() -> 'HELLO'
- 'HELLO'.lower() -> 'hello'
**Output:** hello

iii. `'-'.join('There can be only one'.split())`


- 'There can be only one'.split() -> ['There', 'can', 'be', 'only', 'one']
- '-'.join(...) -> 'There-can-be-only-one'
**Output:** There-can-be-only-one

Q9. Discuss different paths of file system.

There are two main types of file paths in any operating system:

1. **Absolute Path**:
- Specifies the full path from the root directory.
- Works independently from the current working directory.
- Example (Linux): /home/user/docs/file.txt
- Example (Windows): C:\Users\Admin\Documents\file.txt

2. **Relative Path**:
- Starts from the current working directory.
- Does not begin with root or drive letter.
- Example: ./docs/file.txt or ../file.txt

Python uses modules like `os` and `pathlib` to work with both types.

Q10. Explain the concept of file path. Also discuss absolute and relative file path.

**File Path** is a way to specify the location of a file in the computer system.

**Absolute Path:**
- Gives the full address of the file from the root.
- Example: C:\\Users\\Admin\\file.txt (Windows)
- Example: /home/user/file.txt (Linux)

**Relative Path:**
- Describes the location of the file relative to the current directory.
VTU - Introduction to Python Programming - Module 1

CO Mapped: CO1 - Demonstrate proficiency in handling loops and the creation of functions

- Example: ../file.txt or ./subdir/file.txt

**Why it's important?**


- Helps in locating files for reading/writing operations.
- Useful in both local and web-based file systems.

**Python Usage:**
```python
import os
print(os.path.abspath("myfile.txt")) # Convert to absolute
```

You might also like