Strings in Python
Published: 2023-05-11 06:26:42

-  In Python, a string is a sequence of characters enclosed in quotes. Strings can be created using single quotes (`'`), double quotes (`"`), or triple quotes (`'''` or `"""`) depending on the requirements.
- Strings in Python are immutable, meaning once they are created, their contents cannot be changed.
-   However, you can create a new string by concatenating existing strings or by using string methods.

```python
>>> string1 = 'Hello, World!'
>>> string2 = "This is a string."
>>> string3 = '''This is a
... multi-line string.'''
>>> string4 = """This is also
... a multi-line string."""
```
## String Methods of Python

### 1. len() :-
This method returns the length of a string.
```python
>>> len(string1)
13
```

###  2. .upper():-

-   This method returns a new string with all characters in uppercase.
```python
>>> string2
'This is a string.'
>>> string2.upper()
'THIS IS A STRING.'
```
###  3. .lower() :-
-   This method returns a new string with all characters in lowercase.
```python
>>> string2
'This is a string.'
>>> string2.lower()
'this is a string.'
```

###  4. .capitalize() :-
This method returns a new string with the first character capitalized.
```python
>>> string4 = string2.lower()
>>> string4
'this is a string.'
>>> string4.capitalize()
'This is a string.'
>>> string4
'this is a string.'
```
###  5. .title():-
This method returns a new string with the first character of each word capitalized.
```python
>>> string3 = "This is a small string"
>>> string3.title()
'This Is A Small String'
```
###  6. split(delimiter) :-
This method returns a list of strings by splitting the original string at each occurrence of a specified delimiter.
By default, delimiter is space
example:-
```python
>>> string3
'This is a small string'
>>> string3.split()
['This', 'is', 'a', 'small', 'string']
>>> string3 = 'This_is_a_string'
>>> string3.split('_')
['This', 'is', 'a', 'string']
```
### 7. count(x) :-
The `.count()` method returns the number of times an item appears in the string.
```python
>>> string1 = 'Lets count a many times aaaaa'
>>> string1.count('a')
7
```
### 8. .strip() :-
This method returns the string with both leading and trailing characters.
```python
>>> string1 = '   this is   a string  '
>>> string1.strip()
'this is   a string'
```
### 9. .endswith() :-

This method returns true if the string ends with the specified value.

```python
>>> string1.endswith('aaaaa')
True
>>> string1.endswith('aaaaaa')
False
```
### 10. .find() :-
-   This method searches the string for a specified value and returns the position of where it was found.
```python
>>> string1
'Lets count a many times aaaaa'
>>> string1.find('m')
13
>>> string1.find('tim')
18
```

### 11. .startswith() :-
-   This method returns true if the string starts with the specified value
```python
>>> string1
'Lets count a many times aaaaa'
>>> string1.startswith('L')
True
>>> string1.startswith('l')
False
```

### 12. .swapcase() :-
-   This method swaps cases, lower case becomes upper case and vice versa
```python
>>> string1 = "LETS SEE swap of casing"
>>> string1.swapcase()
'lets see SWAP OF CASING'
```

### 13. .isalnum() :-
-   This method returns True if all characters in the string are alphanumeric
```python
>>> string1 = "abc 89a"
>>> string1.isalnum()
False
>>> string1 = "abc89a"
>>> string1.isalnum()
True
```

### 14. .isalpha() :-
-   This method returns True if all characters in the string are in the alphabet
```python
>>> string1
'abc89a'
>>> string1.isalpha()
False
>>> string1='abcd'
>>> string1.isalpha()
True
```
### 15. .isnumeric() :-
-   This method returns True if all characters in the string are numeric
```python
>>> string1 = "abc89a"
>>> string1.isnumeric()
False
>>> string1 = "89.456"
>>> string1.isnumeric()
True
```
### 16. .isdigit() :-
-   This method returns true if all characters in the string are digits.
```python
>>> string1 = "abc 89a"
>>> string1.isdigit()
False
>>> string1 = "89"
>>> string1.isalnum()
True
```
### 17. .islower() :-
-   This method returns True if all characters in the string are in lowercase.
```python
>>> string1 = "abAde"
>>> string1.islower()
False
>>> string1 = "abcdae"
>>> string1.islower()
True
```
### 18. .isupper() :-
-   This method returns True if all characters in the string are alphanumeric
```python
>>> string1 = "abac"
>>> string1.isupper()
False
>>> string1 = "ABCA"
>>> string1.isupper()
True
```

### 19. .replace() :-
-   This method returns a string where a specified value is replaced with a specified value
```python
>>> string1='abcd'
>>> string2 = 'efg'
>>> string1.replace('d' , string2)
'abcefg'
```
### 20. .format() :-
-   This method formats specified values in a string
```python
>>> txt = "For only {price:.2f} dollars!"
>>> txt.format(price = 49)
'For only 49.00 dollars!'
```

## References

[Python docs official](https://docs.python.org/3/tutorial/datastructures.html)
[W3Schools](https://www.w3schools.com/python/python_ref_string.asp)