PYTHON : STRING METHODS
Tags: python
Published: 2023-02-15 09:53:28

**Some of the most important string methods in python are :**

1. capitalize() : Convert the first letter of the given list
   an uppercase letter and returns a new string.

```python
# String   
s="cat"
# using capitalize()
z=s.capitalize()
print(z)
# Output : Cat
```

2.casefold() : Returns a string where all the elements are lower case.

```python
# string
s=CAt
# using casefold()
z=s.casefold()
print(z)
# Output : cat
```
3.center() : Returns a string aligned by the given character spaces.

```python 
s=cat
z=s.center(10)
print(z)
# Output :    cat
```

4.count() : Returns the number of times a given substring has occured in the given string.

```python
s="catcat"
n=s.count('cat')
print(n)
# Output :  2
```

5.encode() : Encodes the string with the given encoding type.

```python
txt = "My name is Sam."
print(txt.encode(encoding="utf-8",errors="ignore"))
# Output : My name is Sam.
```

6.endswith() : Returns False if the string does not end with the given substring.

```python
txt="Apple"
print(txt.endswith('le'))
# Output : True
```

7.find(): Finds the first occurrence of the given substring.

```python
txt='batballcat'
n=txt.find('cat')
print(n)
# Output : 7
```

8.format() : Formats the given value and insert them inside the string's placeholder.

```python
txt = "I have {x} apples"
print(txt.format(x=20))
# Output : I have 20 apples.
```

9.format_map() : Formats the given dictionary value and insert the key and the value in the string's placeholder.

```python
a = {'x':'John', 'y':'Doe'}  
# Use of format_map() function
print("{x} {y} is a stranger.".format_map(a))
# Output : John Doe is a stranger.
```

10.index() : Returns the index occurrence of the first occurrence of the given substring.

```python
a='batcatrat'
print(a.index('rat'))
# Output : 6
```

11.isalnum() : Checks whether all the characters in the string is alphanumeric or not.

```python
a='batcatrat123'
print(a.isalnum())
# Output : True
```
12.isprintable() : Checks whether all the characters are printable or not.Also returns True if the list is empty.

```python
a="Hello"
print(a.isprintable())
# Output : True

a="Hello\n"
print(a.isprintable())
# Output : False
```

13.isnumeric() : Checks whether all the characters are numeric or not.

```python
a='1234'
print(a.isnumeric())
# Output : True
```

14.islower() : Checks whether all the characters are lowercase or not.

```python
a='Bat'
print(a.islower())
# Output : False
```

15.isidentifier() : Checks whether the string is valid identifier or not.

```python
a='1234_a'
print(a.isidentifier())
# Output : False
```
16.isdigit() : Checks whether all the characters are digit or not.

```python
a='1234'
print(a.isdigit())
# Output : True
```

17.isdecimal() : Checks whether all the characters in the string are decimal or not.

```python
a='12'
print(a.isdecimal())
# Output : True
```

18.isalpha() : Checks whether all the characters in the string are alphabets or not.

```python
a='abd'
print(a.isalpha())
# Output : True
```

19.isspace() : Checks whether all the characters in the string are space or not.

```python
a='  '
print(a.isspace())
# Output : True
```

20.zfill() : Places the given number of zeroes at the left side of string.

```python
z='cat'
x=z.fill(3)
print(x)
# Output : 000cat
```

21.upper() : Converts all the lowercase characters to uppercase.

```python
z='cAt'
print(z.upper())
# Output : CAT
```

22.title() : Converts the string to title case.

```python
z='cAt'
print(z.title())
# Output : Cat
```

23.swapcase() : Converts all the uppercase characters to lowercase and vice versa.

```python
z='cAt'
print(z.swapcase())
# Output : CaT
```

24.strip() : Removes the space from the beginning and the end of string 

```python
z='  cat  '
print(len(z.strip()))
# Output : 3
```

25.startswith() : Checks whether the string starts with 
the given substring.

```python
z='cat'
print(z.startswith('c'))
# Output : True
```

26.splitlines() : Splits the string where each line ends and returns a list.

```python
txt = "Cat\nMouse"
x = txt.splitlines()
print(x)
# Output : ['Cat', 'Mouse']
```

27.rpartition() : Returns a tuple with three elements.Takes a string as an input which becomes the middle element of the tuple.

```python
txt = "catMousedog"
x = txt.rpartition("Mouse")
print(x)
# Output : ('cat', 'Mouse', 'dog')
```

28.rjust() : Right aligns the string by the given width.

```python
txt = "cat"
print(txt.rjust(10))
# Output :       cat
```

29.rfind() : Returns the highest index of the given substring.

```python
txt='catbatcat'
print(txt.rfind('cat'))
# Output : 6
```

30.replace() : Replaces all the occurrence of the given substring with the desired substring and returns a new substring.

```python
txt="bznznz"
a=txt.replace('z','a')
print(a)
# Output : banana
```
31.lstrip() : Removes the spaces from the beginning and returns a string.

```python
txt="  cat "
l=txt.lstrip()
print(len(l))
# Output : 4
```

32.lower() : Converts all the uppercase letters to lowercase.

```python
txt='CAt'
newtxt=txt.lower()
print(newtxt)
```

33.join() : Returns a concatenated string.

```python
mytuple=('bat','cat','ball')
space=' '
x=space.join(mytuple)
print(x)
# Output : bat cat ball
```