String Methods in Python
Tags: python, programming
Published: 2023-05-11 08:53:20

* ### lower()

    Converts all uppercase characters in a string into lowercase.

    ```python
    my_str="Hello World"
    print(my_str.lower()) # hello world
    print(my_str) # Hello World
    ```

* ### islower()

    Checks if all characters in the string are lowercase.

    ```python
    str = 'Hello World'
    print(str.islower()) # False

    str = 'hello world'
    print(str.islower()) # True
    ```

* ### casefold()

    Returns a string where all the characters are lower case.

    ```python
    my_str="HeLLo World"
    print(my_str.casefold()) # hello world
    ```
    
* ### upper()

    Converts all lowercase characters in a string into uppercase.

    ```python
    my_str="Hello World"
    print(my_str.upper()) # HELLO WORLD
    print(my_str) # Hello World
    ```

* ### isupper()

    Checks if all characters in the string are uppercase.

    ```python
    str = 'Hello World'
    print(str.isupper()) # False

    str = 'HELLO'
    print(str.isupper()) # True
    ```

* ### title()

    Convert string to title case

    ```python
    my_str="hello world"
    print(my_str.title()) # Hello World
    ```

* ### istitle()

    Returns “True” if the string is a title cased string.

    ```python
    str = 'Hello World'
    print(str.istitle()) # True

    str = 'hello world'
    print(str.istitle()) # False
    ```

* ### capitalize()

    Convert the first character of a string to capital (uppercase) letter.

    ```python
    my_str="hello world"
    print(my_str.capitalize()) # Hello world
    ```
    
* ### swapcase()

    Swap the cases of all characters in a string.

    ```python
    my_str="Hello World"
    print(my_str.swapcase()) # hELLO wORLD
    ```
    
* ### center()

    Pad the string with the specified character upto specified length.

    ```python
    my_str="Hello World"
    print(my_str.center(16,'a')) # aaHello Worldaaa
    print(my_str.center(20,'a')) # aaaaHello Worldaaaaa
    ```
    
* ### count()

    Returns the number of occurrences of a substring in the string.

    ```python
    my_str="Hello World Hello Hello"
    print(my_str.count("Hello")) # 3
    print(my_str.count('l')) # 7
    ```
    
* ### encode()

    Encodes strings with the specified encoded scheme. 
    In case of failure, it raises a UnicodeDecodeError exception.

    ```python
    my_str="Hello World"
    # change encoding to utf-8
    print(my_str.encode()) # b'Hello World'
    ```
    
* ### endswith()

    Returns “True” if a string ends with the given suffix.

    ```python
    my_str="Hello World"
    print(my_str.endswith("World")) # True
    ```
    
* ### expandtabs()

    Specifies the amount of space to be substituted with the “\t” symbol in the string. Default tabsize is 8.

    ```python
    str = 'Hello\tWorld'
    print(str.expandtabs()) # Hello   World
    print(str.expandtabs(20)) # Hello               World
    ```
    
* ### find()

    Returns the lowest index of the substring if it is found.

    ```python
    str = 'Hello World'
    print(str.find("World")) # 6
    print(str.find("Galaxy")) # -1
    ```

* ### index()

    Returns the position of the first occurrence of a substring in a string.
    If substring doesn't exist inside the string, it raises a ValueError exception.

    ```python
    str = 'Hello World'
    print(str.index("World")) # 6
    ```

* ### format()

    Formats the string for printing it to console.

    ```python

    name="Mike"

    str = 'Hello {}!'
    print(str.format(name)) # Hello Mike!

    # We can also do it this way
    str2= f"Hello {name}!"
    print(str2) # Hello Mike!
    ```
    
* ### format_map()

    Formats specified values in a string using a dictionary.

    ```python
    coordinates = {'x':4,'y':-5, 'z': 0}
    print('{x} {y} {z}'.format_map(coordinates)) # 4 -5 0
    ```
    
* ### isalnum()

    Checks whether all the characters in a given string is alphanumeric or not.

    ```python
    str = 'Hello World'
    print(str.isalnum()) # False

    str = 'HelloWorld'
    print(str.isalnum()) # True

    str = 'HelloWorld1100'
    print(str.isalnum()) # True
    ```
    
* ### isalpha()

    Returns “True” if all characters in the string are alphabets.

    ```python
    str = 'Hello World'
    print(str.isalpha()) # False

    str = 'HelloWorld'
    print(str.isalpha()) # True

    str = 'HelloWorld1100'
    print(str.isalpha()) # False
    ```

* ### isnumeric()

    Returns “True” if all characters in the string are numeric characters.

    ```python
    str = 'Hello World'
    print(str.isnumeric()) # False

    str = '12131'
    print(str.isnumeric()) # True
    ```

* ### isdecimal()

    Returns true if all characters in a string are decimal.

    ```python
    str = 'Hello World'
    print(str.isdecimal()) # False

    str = '1234567'
    print(str.isdecimal()) # True

    str = '9866asdf3s'
    print(str.isdecimal()) # False
    ```
    
* ### isdigit()

    Returns “True” if all characters in the string are digits.

    ```python
    str = '1234567'
    print(str.isdigit()) # True

    str = '9866asdf3s'
    print(str.isdigit()) # False
    ```
    
* ### isidentifier()

    The isidentifier() method returns True if the string is a valid identifier in Python.

    ```python
    str = 'Hello World'
    print(str.isidentifier()) # False

    str = 'while'
    print(str.isidentifier()) # True

    str = 'Python'
    print(str.isidentifier()) # True
    ```
    
* ### isprintable()

    Returns “True” if all characters in the string are printable or the string is empty.

    ```python
    str = 'Hello World'
    print(str.isprintable()) # True

    str = 'Hello World\n'
    print(str.isprintable()) # False
    ```
    
* ### isspace()

    Returns “True” if all characters in the string are whitespace characters.

    ```python
    s = '   \t'
    print(s.isspace()) # True

    s = ' a '
    print(s.isspace()) # False

    s = ''
    print(s.isspace()) # False
    ```
    
* ### join()

    The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.

    ```python
    text = ['Python', 'is', 'a', 'fun', 'programming', 'language']
    print(' '.join(text)) # Python is a fun programming language
    print('_'.join(text)) # Python_is_a_fun_programming_language
    ```
    
* ### ljust()

    Left aligns the string according to the width specified

    ```python
    ```
    
* ### lstrip()

    Returns the string with leading characters removed

    ```python
    ```
    
* ### maketrans()

    Returns a translation table

    ```python
    ```
    
* ### partition()

    Splits the string at the first occurrence of the separator 

    ```python
    ```
    
* ### replace()

    Replaces all occurrences of a substring with another substring

    ```python
    ```
    
* ### rfind()

    Returns the highest index of the substring

    ```python
    ```
    
* ### rindex()

    Returns the highest index of the substring inside the string

    ```python
    ```
    
* ### rjust()

    Right aligns the string according to the width specified

    ```python
    ```
    
* ### rpartition()

    Split the given string into three parts

    ```python
    ```
    
* ### rsplit()

    Split the string from the right by the specified separator

    ```python
    ```
    
* ### rstrip()

    Removes trailing characters

    ```python
    ```
    
* ### splitlines()

    Split the lines at line boundaries

    ```python
    ```
    
* ### startswith()

    Returns “True” if a string starts with the given prefix

    ```python
    ```
    
* ### strip()

    Returns the string with both leading and trailing characters

    ```python
    ```

* ### translate()

    Modify string according to given translation mappings

    ```python
    ```
    
* ### zfill()

    Returns a copy of the string with ‘0’ characters padded to the left side of the string

    ```python
    ```

Reference

* https://docs.python.org/3/library/string.html
* https://www.geeksforgeeks.org/python-string-methods/
* https://www.programiz.com/python-programming/methods/string