📓 Others

In [1]:

index(sub[, start[, end]])

2025-05-29 13:18

Returns the index of the substring or raises an error.

### index(sub[, start[, end]])

**Description:**
Returns the index of the first occurrence of the substring. Raises ValueError if not found.

**Example:**
“`python
text = “hello”
print(text.index(“e”)) # Output: 1
“`

**Use Case:**
Like find(), but ensures the substring exists by raising an error if it doesn’t.

In [2]:

capitalize()

2025-05-29 13:18

Converts the first character to uppercase.

capitalize()

text = "hello world"
print(text.capitalize()) # Output: 'Hello world'

 

**Description:**
Converts the first character of a string to uppercase and the rest to lowercase.

**Example:**
“`python
text = “hello world”
print(text.capitalize()) # Output: ‘Hello world’
“`

**Use Case:**
Useful for formatting strings, especially when dealing with titles or sentences.

In [3]:

center(width[, fillchar])

2025-05-29 13:18

Centers the string within the specified width.

### center(width[, fillchar])

**Description:**
Returns a centered string of length width. Padding is done using the specified fill character (default is space).

**Example:**
“`python
text = “hello”
print(text.center(11, “-“)) # Output: ‘—hello—‘
“`

**Use Case:**
Helpful in creating formatted outputs, such as console tables or UI elements.

In [4]:

count(sub[, start[, end]])

2025-05-29 13:18

Returns the number of occurrences of a substring.

### count(sub[, start[, end]])

**Description:**
Returns the number of non-overlapping occurrences of substring sub in the string.

**Example:**
“`python
text = “banana”
print(text.count(“a”)) # Output: 3
“`

**Use Case:**
Used to analyze the frequency of characters or substrings in text data.

In [5]:

decode()

2025-05-29 13:18

Decodes the string using the specified codec.

### decode()

**Description:**
Decodes bytes to string using the specified encoding. Only applicable to byte objects.

**Example:**
“`python
byte_data = b’hello’
print(byte_data.decode(‘utf-8’)) # Output: ‘hello’
“`

**Use Case:**
Used when handling byte streams or encoded data from files or networks.

In [6]:

encode([encoding[, errors]])

2025-05-29 13:18

Encodes the string using the specified encoding.

### encode([encoding[, errors]])

**Description:**
Encodes the string using the specified encoding format.

**Example:**
“`python
text = “hello”
print(text.encode(‘utf-8′)) # Output: b’hello’
“`

**Use Case:**
Useful when writing text to binary files or sending text over a network.

In [7]:

endswith(suffix[, start[, end]])

2025-05-29 13:18

Checks if the string ends with the specified suffix.

### endswith(suffix[, start[, end]])

**Description:**
Returns True if the string ends with the specified suffix.

**Example:**
“`python
text = “example.txt”
print(text.endswith(“.txt”)) # Output: True
“`

**Use Case:**
Commonly used in file type checking or URL validation.

In [8]:

expandtabs([tabsize])

2025-05-29 13:18

Replaces tabs with spaces using the given tab size.

### expandtabs([tabsize])

**Description:**
Returns a copy of the string where all tab characters are replaced by spaces.

**Example:**
“`python
text = “1 2 3”
print(text.expandtabs(4)) # Output: ‘1 2 3’
“`

**Use Case:**
Useful in formatting outputs or aligning text.

In [9]:

find(sub[, start[, end]])

2025-05-29 13:18

Returns the lowest index of the substring if found.

### find(sub[, start[, end]])

**Description:**
Returns the lowest index where the substring is found. Returns -1 if not found.

**Example:**
“`python
text = “hello world”
print(text.find(“world”)) # Output: 6
“`

**Use Case:**
Used for locating substrings without raising errors.

In [10]:

format(*args, **kwargs)

2025-05-29 13:18

Formats the string using the given arguments.

### format(*args, **kwargs)

**Description:**
Inserts specified values into placeholders in a string.

**Example:**
“`python
text = “Hello, {}. You are {} years old.”
print(text.format(“Alice”, 30)) # Output: ‘Hello, Alice. You are 30 years old.’
“`

**Use Case:**
Used for dynamic string formatting in output or logs.