String Methods in Python
Published: 2023-02-15 06:51:53

## String Methods in Python

### 1. upper():
Returns a copy of string in upper-case.

**syntax:** str.upper()
```py
string = 'My name is Shubham.'

print(string.upper())
```
**output:** MY NAME IS SHUBHAM.

### 2. lower():
Returns a copy of string in lower-case.

**syntax:** str.lower()
```py
string = 'My name is Shubham.'

print(string.lower())
```
**output:** 'my name is shubham.'

### 3. swapcase()
Returns a copy of string in swapped-case.

**syntax:** str.swapcase()
```py
string = 'My name is Shubham.'

ptint(string.swapcase())
```
**output:** 'mY NAME IS sHUBHAM.'

### 4. capitalize()
Returns a copy of string with first letter in upper-case and rest in lower-case.

**syntax:** str.capitalize()
```py
string = 'My name is Shubham.'

print(string.capitalize())
```
**output:** My name is shubham.

### 5. title()
Returns a copy of string in title-case.

**syntax:** str.title()
```py
string = 'My name is Shubham.'

print(string.title())
```
**output:** My Name Is Shubham.

### 6. isupper()
Returns boolean value if all the letters in string are upper-case.

**syntax:** str.isupper()
```py
string1 = 'MY NAME IS SHUBHAM.'
string2 = 'My name is Shubham.'

print(string1.isupper())
print(string2.isupper())
```
**output:** True, False

### 7. islower()
Returns boolean value if all the letters in string are lower-case.

**syntax:** strislower()
```py
string1 = 'my name is shubham.'
string2 = 'My name is Shubham.'

print(string1.islower())
print(string2.islower())
```
**output:** True, False

### 8. isalnum():
Returns boolean value if all the characters in the string are alpha-numeric.

**syntax:** str.isalnum()
```py
string1 = 'My1name2is3Shubham4'
string2 = 'My name is Shubham.'

print(string1.isalnum())
print(string2.isalnum())
```
**output:** True, False

### 9. isalpha():
Returns boolean value if all the characters in the string are alphabet.

**syntax:** str.isalpha()
```py
string1 = 'MyNameIsShubham'
string2 = 'My name is Shubham.'

print(string1.isalnum())
print(string2.isalnum())
```
**output:** True, False

### 10. isdigit():
Returns boolean value if all the characters in the string are digit.

**syntax:** str.isdigit()
```py
string1 = '1234'
string2 = 'My name is Shubham.'

print(string1.isdigit())
print(string2.isdigit())
```
**output:** True, False

### 11. startswith(str):
Returns boolean if the string starts with given sub-string.

**syntax:** str.startswith(sub_string)
```py
string = 'My name is Shubham.'

print(string.startswith('My'))
print(string.startswith('Shubham.'))
```
**output:** True, False

### 12. endswith(str):
Returns boolean if the string ends with given sub-string.

**syntax:** str.endswith(sub_string)
```py
string = 'My name is Shubham.'

print(string.endswith('Shubham.'))
print(string.endswith('My'))
```
**output:** True, False

### 13. count(str):
Returns the count of the number of times a substring appears in the string.

**syntax:** str.count(sub_str)
```py
string = 'My name is Shubham and my favorite coding language is Python.'

print(string.count('is'))
```
**output:** 2

### 14. find(str):
Returns the index of first occurance of given substring in string. Return -1 if given substring is not present in string.

**syntax:** str.found(sub_str)
```py
string = 'My name is Shubham.'

print(string.find('name'))
print(string.find('years'))
```
**output:** 3, -1

### 15. replace(str1, str2):
Returns a string replacing substring in the first parameter from the string with substring in the second parameter.

**syntax:** str.replace(sub_str1, sub_str2)
```py
string = 'My name is Shubham.'

print(string.replace('My name is', 'Myself'))
```
**output:** Myself Shubham.

### 16. split():
Returns a list of words splitted from the given string.

**syntax:** str.split()
```py
string = 'My name is Shubham.'

print(string.split())
```
**output:** ['My', 'name', 'is', 'Shubham.']

### 17. join(itr):
Returns a string joined from the elements of given iterable.

**syntax:** "*".join(iterable)
```py
l = ['My', 'name', 'is', 'Shubham.']

print(" ".join(l))
```
**output:** My name is Shubham.

### **REFERENCES**

+ [Python Documentation](https://docs.python.org/3/library/stdtypes.html)