Python String Methods
Tags: python, pythonstring
Published: 2023-02-16 06:10:40

Python has a set of built-in methods that you can use on strings.
All string methods returns new values. They do not change the original string.

1) **capitalize()**
   
The <span style="color: orange">capitalize()</span> converts the first character to upper case.
```py
txt = "hello, and welcome to my world."
x = txt.capitalize()
print(x) # Hello, and welcome to my world.
```

2) **count(value)**

The <span style="color: orange">count()</span> method returns the number of times a specified value appears in the string.
```py
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x) # 2
```

3) **find(value)**

Searches the string for a specified value and returns the position of where it was found.

The <span style="color: orange">find()</span> method finds the first occurrence of the specified value. 
It returns -1 if the value is not found.
```py
txt = "Hello, welcome to my world."
x = txt.find("welcome")
print(x) # 7
```

4) **String Slicing**

To obtain a substring from a given string we use slicing. 

We need to specify the start index and the end index seperated by a colon.
```py
name = "Hey, This is Anirban"
print(name[13:20]) # Anirban
print(name[13:]) # Anirban
print(name[-7:]) # Anirban
```

5) **replace(oldvalue, newvalue)**

The <span style="color: orange">replace()</span> method returns a string where a specified value is replaced with another value given.
```py
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x) # I like apples
```

6) **strip(characters)**

The <span style="color: orange">strip()</span> method removes any whitespaces at the beginning and ending of a string and returns the trimed string.

Optionally we can even give a character to be removed from the string.
```py
txt = "   Mount Blue   "
x = txt.strip()
print(x) # Mount Blue
```

7) **upper()**

The <span style="color: orange">upper()</span> converts a string into uppercase
```py
txt = "Hello my friends"
x = txt.upper()
print(x) # HELLO MY FRIENDS
```

8) **lower()**

The <span style="color: orange">lower()</span> converts a strubg into lowercase
```py
txt = "Hello my FRIENDS"
x = txt.lower()
print(x) # hello my friends
```

9) **join(iterable)**

The <span style="color: orange">join()</span> converts the elements of an iterable into a string
```py
my_tuple = ("John", "Anirban", "Roshan")
x = ' '.join(my_tuple)
print(x) # John Anirban Roshan
```

10) **swapcase()**

The <span style="color: orange">swapcase()</span> swap the case of the elements of a string from upper to lower and vice versa.
```py
name = "Anirban"
x = name.swapcase()
print(x) # aNIRBAN
```

11) **islower()** and **isupper()**

<span style="color: orange">islower()</span> returns true if all the characters in the string are in lowercase

<span style="color: orange">isupper()</span> returns true if all the characters in the string are in uppercase
```py
name = "anirban"
print(name.islower()) # True
print(name.isupper()) # False
```

12) **isalnum()**, **isalpha()** and **isdigit()**

<span style="color: orange">isalnum()</span> returns true if all the characters of the string are alphanumeric in nature

<span style="color: orange">isalpha()</span> returns true if all the characters of the string are alphabetic in nature 

<span style="color: orange">isdigit()</span> returns true if all the characters of the string are digits

```py
name = "Company12"
print(name.isalnum()) # True
print(name.isalpha()) # False
print(name.isdigit()) # False
```

13) **title()**

The <span style="color: orange">title()</span> converts the first character of each word to upper case

```py
txt = "Welcome to my world"
x = txt.title()
print(x) # Welcome To My World
```

## References:

* [Python String, W3Schools](https://www.w3schools.com/python/python_ref_string.asp)
* [Python String, GeeksforGeeks](https://www.geeksforgeeks.org/python-string-methods/)