String methods in Python

In addition to the operations defined for all sequential data types, Python provides string-specific methods, some of which are listed here.

Code Explanation

  1. The first line of code creates a string variable myString with the value ” Hello world “.
  2. The split method is used on the myString variable in the next line of code. The split method returns a list of the words in the string, using whitespace as the delimiter string. The resulting list is stored in the variable myList.
  3. The find method is used on the myString variable in the next line of code. The find method returns the lowest index in myString where the substring “o” is found. The result is stored in the variable a. If the substring is not found, the find method returns -1.
  4. The rfind method is used on the myString variable in the next line of code. The rfind method returns the highest index in myString where the substring “o” is found. The result is stored in the variable r. If the substring is not found, the rfind method returns -1.
  5. The replace method is used on the myString variable in the next line of code. The replace method returns a copy of the string with all occurrences of the substring “world” replaced by “user”. The resulting string is stored in the variable b.
  6. The lower method is used on the myString variable in the next line of code. The lower method returns a copy of the string converted to lowercase. The resulting string is stored in the variable l.
  7. The upper method is used on the myString variable in the next line of code. The upper method returns a copy of the string converted to uppercase. The resulting string is stored in the variable u.
  8. The strip method is used on the myString variable in the final line of code. The strip method returns a copy of the string with leading and trailing whitespace removed. The resulting string is stored in the variable s.

In summary, this code demonstrates the use of several string methods in Python, including split, find, rfind, replace, lower, upper, and strip.

myString = " Hello world "

# Return a list of the words in the string, using sep as the delimiter string.
myList = myString.split()
print(myList)

# S.find(sub[, start[, end]]) -> int
# Return the lowest index in S where substring sub is found,
# such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.
# Return -1 on failure.
a = myString.find("o")
print(a)

# S.rfind(sub[, start[, end]]) -> int
# Return the highest index in S where substring sub is found,
# such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.
# Return -1 on failure.
r = myString.rfind("o")
print(r)

# Return a copy with all occurrences of substring old replaced by new.
b = myString.replace("world", "user")
print(b)

# Return a copy of the string converted to lowercase.
l = myString.lower()
print(l)

# Return a copy of the string converted to uppercase.
u = myString.upper()
print(u)

# Return a copy of the string with leading and trailing whitespace removed.
# If chars is given and not None, remove characters in chars instead.
s = myString.strip()
print(s)
Output
['Hello', 'world']
5
8
 Hello user 
 hello world 
 HELLO WORLD 
Hello world