Python Substring

In Python, you can get a substring from a string using slicing. The syntax for slicing a string is as follows:

string[start:end]

Where start is the index of the first character of the substring, and end is the index of the character immediately following the last character of the substring.

For example, to get a substring containing the first three characters of a string s, you would use:

s[0:3]

This would return the substring from index 0 to index 2 (inclusive), which contains the first three characters of the string s.

You can also omit the start or end index to slice from the beginning or end of the string, respectively. For example, to get a substring containing the first three characters of a string s, you can use:

s[:3]

This is equivalent to s[0:3]. Similarly, to get a substring containing the last three characters of a string s, you can use:

s[-3:]

This would return the substring from the third last character to the end of the string, which contains the last three characters of the string s.

Pre-defined String Methods:

Python has several pre-defined string methods that allow you to perform various operations on strings. Here are some of the most commonly used string methods in Python:

  1. upper(): Returns a new string with all characters in uppercase.
s = "hello"
print(s.upper()) # Output: HELLO
  1. lower(): Returns a new string with all characters in lowercase.
s = "HELLO"
print(s.lower()) # Output: hello
  1. capitalize(): Returns a new string with the first character in uppercase and the rest in lowercase.
s = "hello world"
print(s.capitalize()) # Output: Hello world
  1. strip(): Returns a new string with leading and trailing whitespace removed.
s = "   hello   "
print(s.strip()) # Output: hello
  1. replace(): Returns a new string with all occurrences of a substring replaced by another substring.
s = "hello world"
print(s.replace("world", "Python")) # Output: hello Python
  1. split(): Returns a list of substrings separated by a given delimiter (by default, whitespace).
s = "hello world"
print(s.split()) # Output: ["hello", "world"]
  1. join(): Concatenates a list of strings with a given delimiter.
lst = ["hello", "world"]
delimiter = " "
print(delimiter.join(lst)) # Output: "hello world"

These are just a few examples of the many pre-defined string methods available in Python.

What is a Substring:

A substring is a contiguous sequence of characters within a string. In other words, a substring is a portion of a larger string that consists of one or more characters. For example, “hello” is a substring of the string “hello world”, and “world” is another substring of the same string.

In Python, you can get a substring from a string using slicing. The syntax for slicing a string is as follows:

string[start:end]

Where start is the index of the first character of the substring, and end is the index of the character immediately following the last character of the substring.

Working of negative index:

In Python, negative indexing allows you to access elements of a sequence (such as a string or a list) from the end of the sequence.

The index of the last element in the sequence is -1, the index of the second-to-last element is -2, and so on. For example, if s is a string with length 5, then s[-1] would refer to the last character of the string, s[-2] would refer to the second-to-last character, and so on.

Here’s an example to illustrate how negative indexing works:

s = "hello"
print(s[-1])   # Output: 'o'
print(s[-2])   # Output: 'l'
print(s[-3])   # Output: 'l'

In this example, s is a string with the value “hello”. The first print() statement prints the last character of the string (which is ‘o’), the second print() statement prints the second-to-last character (which is ‘l’), and the third print() statement prints the third-to-last character (which is also ‘l’).

Negative indexing can be especially useful when you don’t know the length of a sequence or when you want to access elements from the end of a sequence without having to compute the length.