VTU - Introduction to Python Programming - Module 1
CO Mapped: CO1 - Demonstrate proficiency in handling loops and the creation of functions
Q1. With example code explain join() and split() string method.
### `join()` Method:
The `join()` method takes all items in an iterable and joins them into one string. A string must be specified as the
separator.
**Syntax:** `'separator'.join(iterable)`
**Example:**
words = ['Python', 'is', 'fun']
sentence = ' '.join(words)
print(sentence)
**Output:**
Python is fun
**Explanation:** The list of words is joined using a space as a separator.
### `split()` Method:
The `split()` method splits a string into a list where each word is a list item. The default separator is any whitespace.
**Syntax:** `string.split(separator, maxsplit)`
**Example:**
text = 'Welcome to Python Programming'
words = text.split()
print(words)
**Output:**
['Welcome', 'to', 'Python', 'Programming']
**Explanation:** The string is split into individual words.
Q2. Explain the following string methods with example: i) join() ii) islower() iii) strip() iv) center()
**i) join():** Joins elements of an iterable into a single string with a specified separator.
**Example:**
list1 = ['apple', 'banana', 'cherry']
print(', '.join(list1))
**Output:** apple, banana, cherry
VTU - Introduction to Python Programming - Module 1
CO Mapped: CO1 - Demonstrate proficiency in handling loops and the creation of functions
**ii) islower():** Returns True if all characters in a string are lowercase.
**Example:**
s = 'python'
print(s.islower())
**Output:** True
**iii) strip():** Removes leading and trailing whitespaces.
**Example:**
s = ' Hello Python '
print(s.strip())
**Output:** Hello Python
**iv) center():** Returns a centered string of specified width.
**Example:**
s = 'Python'
print(s.center(10, '*'))
**Output:** **Python**
Q3. Explain Python string handling methods with examples: split(), endswith(), ljust(), center(), lstrip()
**split():** Splits string into a list.
Example: 'one two three'.split() -> ['one', 'two', 'three']
**endswith():** Returns True if string ends with specified suffix.
Example: 'python.py'.endswith('.py') -> True
**ljust():** Left-justifies the string.
Example: 'code'.ljust(10, '-') -> 'code------'
**center():** Centers string in a field.
Example: 'text'.center(10, '*') -> '***text***'
**lstrip():** Removes leading whitespaces or characters.
Example: ' Hello'.lstrip() -> 'Hello'
Q4. Explain Python string handling methods with examples: join(), startswith(), rjust(), strip(), rstrip()
**join():** Joins a list into a string.
Example: '-'.join(['a', 'b', 'c']) -> 'a-b-c'
**startswith():** Checks if string starts with a specific value.
VTU - Introduction to Python Programming - Module 1
CO Mapped: CO1 - Demonstrate proficiency in handling loops and the creation of functions
Example: 'hello world'.startswith('hello') -> True
**rjust():** Right-justifies the string.
Example: 'text'.rjust(10, '*') -> '******text'
**strip():** Removes leading/trailing whitespace.
Example: ' data '.strip() -> 'data'
**rstrip():** Removes trailing whitespace.
Example: 'hello '.rstrip() -> 'hello'
Q5. Explain with examples: i) isalpha() ii) isalnum() iii) isspace()
**isalpha():** Returns True if all characters are alphabetic.
Example:
text = 'abc'
print(text.isalpha())
**Output:** True
**isalnum():** Returns True if all characters are alphanumeric (a-z, A-Z, 0-9).
Example:
text = 'abc123'
print(text.isalnum())
**Output:** True
**isspace():** Returns True if all characters are whitespace.
Example:
text = ' '
print(text.isspace())
**Output:** True