Python String Methods
Tags: python, beginners
Published: 2023-02-15 12:53:07

1.format():
The format() method is used to insert values into a string. It takes a string as a template and replaces any placeholders with the corresponding values.
Example:
```
str = "I am {} {} method."
print(str.format("learning", "format()"))
#Output: I am learning format() method.
```

---
2.capitalize():
The capitalize() method is used to convert the first character of a string to uppercase, and all other characters to lowercase.
Example:
```
string = 'python Language'
print(string.capitalize()) #Output: 'Python language'
```
The above line does not change the original string. To modify the original string, we should assign it to the original string variable.

---
3.upper():
The upper() method is used to convert all the characters in a string to uppercase.
```
string = 'python'
string = string.upper()
print(string) #Output: 'PYTHON'
```
We can use isupper() method on a string to check if all the characters in a string are in upper-case. 


---
4.lower():
The lower() method is used to convert all the characters in a string to lowercase.
```
string = 'Python'
print(string.lower()) #Output: 'python'
```
We can use islower() method on a string to check if all the characters in a string are in lower-case.

---
5.strip():
The strip() method removes any leading and trailing spaces from a string.
```
string = '    python    '
print(string.strip()) #Output: 'python'
```
There are also lstrip() and rstrip() methods to strip only the left-end spaces and right-end spaces.

---
6.join():
The join() method is used to join all the string values inside a list with a specified separator.
```
list_of_strings = ['abc','def','ghi']
print(''.join(list_of_strings)) #Output: abcdefghi

print(', '.join(list_of_strings)) #Output: abc, def, ghi
```

---
7.isalnum():
The isalnum() method takes a string as an argument and returns True if all the characters in that string are alpanumeric, otherwise returns False.
```
string = 'Python3'
print(string.isalnum()) #True

string = 'Python&3'
print(string.isalnum()) #False
```

---
8.isalpha():
The isalpha() method takes a string as an argument and returns True if all the characters in that string are alpabets, otherwise returns False.
```
string = 'Python'
print(string.isalpha()) #True

string = 'Python3'
print(string.isalpha()) #False
``` 

---
9.isdigit():
The isdigit() method takes a string as an argument and returns True if all the characters in that string are digits, otherwise returns False.
```
string = '123'
print(string.isdigit()) #True

string = 'python'
print(string.isdigit()) #False
```

---
10.split():
The split() method is used to convert a string to a list of strings by splitting up the string at the specified separator.
```
languages = 'Python,Java,JavaScript'
print(languages.split(',')) # ['Python', 'Java', 'JavaScript']
```

---
11.startswith():
The startswith() method is used to check whether a string starts with specified prefix or not. It returns True if the string starts with the specified string.
```
string = 'Python language'
print(string.startswith('Pyth')) # True

string = 'Python language'
print(string.startswith('pyth    ')) # False
```

---
12.endswith():
The endswith() method is used to check whether a string ends with specified suffix or not. It returns True if the string ends with the specified string.
```
string = 'Python language'
print(string.endswith('uage')) # True
```

---
13.replace():
The replace() method is used to replace every occurrence of matching substring with another string.
```
line = 'Python is a snake'
line = line.replace('snake', 'language') #Python is a language
```

---
14.index():
The index() method returns the index of first occurrence of the given substring. If the given substring is not in the string, then it raises an exception.
We can also give start and end index in which we want to find the substring.
```
line = 'Hello world'
print(line.index('world')) #6

print(line.index('hello')) #ValueError: substring not found

print(line.index('o', 6, 10)) #7
```

---
15.find():
The find() method is same as index() method. But, it returns -1 when substring is not found.
```
line = 'Hello world'
print(line.index('world')) #6

print(line.index('hello')) #-1
```

References:
- [Python String Methods, Programiz](https://www.programiz.com/python-programming/methods/string)
- [Python String Methods, W3Schools](https://www.w3schools.com/python/python_ref_string.asp)