String Methods
Tags: stringsinpython, beginners
Published: 2023-02-16 07:39:18


## What are Strings?


- Strings are the data structures which store a sequence of characters.

- Strings are immutable i.e. they cannot be changed.

- Ex: "Hello"

- "Hello" is a string of characters with characters 'h', 'e', 'l', 'l', and 'o' in it.

- A string can be initialised by single quotes and double quotes. It throws an array if used in combinations of single quotes and double quotes.

- Initialise a string.

    ```python
    >>> s = 'abcde'
    >>> s   #Prints the output
    'abcde'
    ```
- The indexing on a string starts with 0 from the start and -1 from the end.
- Accessing a particular index in a string.

    ```python
    >>> s = 'Hello'
    >>> s[2]
    l
    >>> s[-4]
    e
    ```
- Concatenation in a string.

    ```python
    >>> a = "Python"
    >>> b = "3.7"
    >>> a + " " + b
    "Python 3.7"
    ```
- Slicing in a string is done with the help of square brackets.

- s[start:stop:step]
    
    ```python
    >>> s = 'Pythonist'
    >>> s[2:4]
    th
    ```
## Helper Functions in strings


- **string.capitalize()** 
    
    Returns a copy of a string with the first letter capitalised. 

    ```python
    >>> s.capitalize()
    'Abcde'
    
    ```

- **string.lower()**
    
    Return a copy of a string with all the characters in lowercase.


    ```python
    >>> a = 'Hello WORLD'
    >>> a.lower()
    hello world
    ```

- **string.upper()**
    
    Return a copy of a string with all the characters in lowercase.


    ```python
    >>> a.upper()
    HELLO WORLD   
    ```

- **string.split()**
    
    Returns a list with the space-separated elements. 

    ```python
    >>> a.split()
    ["Hello", "World"]
    ```

- **string.join()**
    
    Returns a string by joining the entire set of characters in a list or a tuple. 

    ```python
    >>> l1 = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
    >>> ''.join(l1)
    'Hello World'
    ```

- **string.swapcase()**
    
    Returns a copy of string but with lowercase letters to uppercase and vice versa.

    ```python
    >>> a = 'Python'
    >>> a.swapcase()
    'pYTHON'
    ```

- **string.casefold()**
    
    Return a copy of a string with all the characters in lowercase. It is the same as the string.lower()


    ```python
    >>> a = 'Hello WORLD'
    >>> a.casefold()
    hello world
    ```

- **string.center(width, fill_char)**
    
    Returns a copy of the string in the centre with added spaces to the right and left. It takes space as a fill character if nothing is given.

    ```python
    >>> s = 'HELLO'
    >>> s.center(30, '-')
    '------------HELLO-------------'

    ```

- **string.count(string)**
    
    Returns an integer value stating the number of occurrences of a string in a string.

    ```python
    >>> s = 'HELLO'
    >>> s.count('L')    
    2
    ```
- **string.endswith(string)**
    
    Returns True if the string end with the specified string.

    ```python
    >>> s = 'Python'
    >>> s.endswith("on")
    True
    ```

- **string.index(string)**
    
    Returns the index of the first occurrence in a string.

    ```python
    >>> s = 'Hello World'
    >>> s.index('o')
    4
    ```

- **string.isalnum()**

    Returns True if all characters in the string are alphanumeric.

    ```python
    >>> s = 'AabcD123'
    >>> s.isalnum()
    True    
    ```

- **string.isalpha()**
      
    Returns True if all characters in the string are in the alphabet.
    ```python
    >>> s = 'abcd'
    >>> s.isalpha()
    True
    ```

- **string.isdecimal()**
    
    Returns True if all characters in the string are decimals.
  
    ```python
    >>> s = '1234'
    >>> s.isdecimal()
    True
    ```

- **string.isdigit()**
    
    Returns True if all characters in the string are digits.

    ```python
    >>> s = '1234'
    >>> s.isdecimal()
    True    
    ```

- **string.islower()**
    
    Returns True if all characters in the string are lowercase.
    
    ```python
    >>> s = 'abcd'
    >>> s.islower()
    True    
    ```

- **string.isupper()**
    
    Returns True if all characters in the string are uppercase.
    
    ```python
    >>> s = 'ABCD'
    >>> s.isupper()
    True    
    ```

## References

- To know more about the helper functions in a string, click on the below link.
    
    [String Methods](https://docs.python.org/3/library/stdtypes.html#string-methods)