Category: Python String functions

  • Python String Encode() Method

    Python encode() method encodes the string according to the provided encoding standard. By default Python strings are in unicode form but can be encoded to other standards also. Encoding is a process of converting text from one standard code to another.

    Python String encode() Method Syntax

    It has the following syntax:

    encode(encoding="utf-8", errors="strict")  

    Parameters

    • encoding : encoding standard, default is UTF-8
    • errors : errors mode to ignore or replace the error messages.

    Both are optional. Default encoding is UTF-8.

    Error parameter has a default value strict and allows other possible values ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ etc too.

    Return Type

    It returns an encoded string.

    Different Examples for Python String Encode() Method 

    Let’s see some examples to understand the encode() method.

    Python String Encode() Method Example 1

    A simple method which encode unicode string to utf-8 encoding standard.

    # Python encode() function example  
    
    # Variable declaration  
    
    str = "HELLO"  
    
    encode = str.encode()  
    
    # Displaying result  
    
    print("Old value", str)  
    
    print("Encoded value", encode)

    Output:

    Old value HELLO
    Encoded value b 'HELLO'
    

    Python String Encode() Method Example 2

    We are encoding a latin character

    � into default encoding.  
    
    # Python encode() function example  
    
    # Variable declaration  
    
    str = "H�LLO"  
    
    encode = str.encode()  
    
    # Displaying result  
    
    print("Old value", str)  
    
    print("Encoded value", encode)

    Output:

    Old value H�LLO
    Encoded value b'H\xc3\x8bLLO'
    

    Python String Encode() Method Example 3

    We are encoding latin character into ascii, it throws an error. See the example below

    # Python encode() function example  
    
    # Variable declaration  
    
    str = "H�LLO"  
    
    encode = str.encode("ascii")  
    
    # Displaying result  
    
    print("Old value", str)  
    
    print("Encoded value", encode)

    Output:

    UnicodeEncodeError: 'ascii' codec can't encode character '\xcb' in position 1: ordinal not in range(128)
    

    Python String Encode() Method Example 4

    If we want to ignore errors, pass ignore as the second parameter.

    # Python encode() function example  
    
    # Variable declaration  
    
    str = "H�LLO"  
    
    encode = str.encode("ascii","ignore")  
    
    # Displaying result  
    
    print("Old value", str)  
    
    print("Encoded value", encode)

    Output:

    Old value H�LLO
    Encoded value b'HLLO'
    

    Python String Encode() Method Example 5

    It ignores error and replace character with ? mark.

    # Python encode() function example  
    
    # Variable declaration  
    
    str = "H�LLO"  
    
    encode = str.encode("ascii","replace")  
    
    # Displaying result  
    
    print("Old value", str)  
    
    print("Encoded value", encode)

    Output:

    Old value H�LLO
    Encoded value b'H?LLO'
  • Python String Count() Method

    It returns the number of occurences of substring in the specified range. It takes three parameters, first is a substring, second a start index and third is last index of the range. Start and end both are optional whereas substring is required.

    Python String count() Method Syntax

    It has the following syntax:

    count(sub[, start[, end]])  

    Parameters

    • sub (required)
    • start (optional)
    • end (optional)

    Return Type

    It returns number of occurrences of substring in the range.

    Different Examples for Python String count() Method

    Let’s see some examples to understand the count() method.

    Python String count() Method Example 1

    Let us take an example to demonstrate the working of the string count() method in Python.

    # Python count() function example  
    
    # Variable declaration  
    
    str = "Hello Pythonapp"  
    
    str2 = str.count('t')  
    
    # Displaying result  
    
    print("occurences:", str2)

    Output:

    occurences: 2
    

    Python String Count() Method Example 2

    Let us take an example to demonstrate the working of the string count() method in Python.

    # Python count() function example  
    
    # Variable declaration  
    
    str = "ab bc ca de ed ad da ab bc ca"  
    
    oc = str.count('a')  
    
    # Displaying result  
    
    print("occurences:", oc)

    Here, we are passing second parameter (start index).

    Python String Count() Method Example 3

    Let us take another example to demonstrate the working of the string count() method in Python.

    # Python count() function example  
    
    # Variable declaration  
    
    str = "ab bc ca de ed ad da ab bc ca"  
    
    oc = str.count('a', 3)  
    
    # Displaying result  
    
    print("occurences:", oc)

    Output:

    occurences: 5
    

    Python String Count() Method Example 4

    The given example is using all three parameters and returning result from the specified range.

    # Python count() function example  
    
    # Variable declaration  
    
    str = "ab bc ca de ed ad da ab bc ca"  
    
    oc = str.count('a', 3, 8)  
    
    # Displaying result  
    
    print("occurences:", oc)

    Output:

    occurences: 1
    

    Python String Count() Method Example 5

    It can count non-alphabet chars also, see the given example.

    # Python count() function example  
    
    # Variable declaration  
    
    str = "ab bc ca de ed ad da ab bc ca 12 23 35 62"  
    
    oc = str.count('2')  
    
    # Displaying result  
    
    print("occurences:", oc)

    Output:

    occurences: 3
  • Python String Center() Method

    Python center() method alligns string to the center by filling paddings left and right of the string. This method takes two parameters, first is a width and second is a fillchar which is optional. The fillchar is a character which is used to fill left and right padding of the string.

    Python String center() Method

    It has the following syntax:

    center(width[,fillchar])  

    Parameters

    • width (required)
    • fillchar (optional)

    Return Type

    It returns modified string.

    Different Examples for Python String center() Method

    Here, we are going to take several examples to demonstrate the string center() Method in Python.

    Python String Center() Method Example 1: default fill char

    Here, we did not pass second parameter. By default it takes spaces.

    # Python center() function example  
    
    # Variable declaration  
    
    str = "Hello Pythonapp"  
    
    # Calling function  
    
    str2 = str.center(20)  
    
    # Displaying result  
    
    print("Old value:", str)  
    
    print("New value:", str2)

    Output:

    Old value: Hello Pythonapp
    New value: Hello Pythonapp
    

    Python String Center() Method Example 2

    Here, we are providing padding char (optional) parameter as #. See the example.

    # Python center() function example  
    
    # Variable declaration  
    
    str = "Hello Pythonapp"  
    
    # Calling function  
    
    str2 = str.center(20,'#')  
    
    # Displaying result  
    
    print("Old value:", str)  
    
    print("New value:", str2)

    Output:

    Old value: Hello Pythonapp
    New value: ##Hello Pythonapp##
    

    Python String Center() Method Example 3

    Let us take another example to demonstrate the string center() method in Python.

    # Python center() function example  
    
    # Variable declaration  
    
    str = "Hello PythonApp"  
    
    if str == "Hell0 PythonApp":  
    
        str2 = str.center(20,'#')  
    
    else:  
    
        str2 = str.center(20,'!')  
    
    # Displaying result  
    
    print("Old value:", str)  
    
    print("New value:", str2)

    Output:

    Old value: Hello PythonApp
    New value: !!Hello PythonApp!!
  • Python String Casefold() Method

    Python Casefold() method returns a lowercase copy of the string. It is more simillar to lowercase method except it revomes all case distinctions present in the string.

    For example in German, ‘β’ is equivelent to “ss”. Since it is already in lowercase, lowercase do nothing and prints ‘β’ whereas casefold converts it to “ss”.

    Python String casefold() Method Syntax

    It has the following syntax:

    casefold()  

      Parameters

      No parameter is required.

      Return Type

      It returns lowercase string.

      Python Version

      This function was introduced in Python 3.3.

      Different Examples for Python Sring casefold() Function

      Here, we are going to take some examples to demonstrate the working of string casefold() function in Python.

      Python String Casefold() Method Example 1

      Let us take an example to illustrate the string casefold() function in Python.

      # Python casefold() function example  
      
      # Variable declaration  
      
      str = "PYTHONAPP"  
      
      # Calling function  
      
      str2 = str.casefold()  
      
      # Displaying result  
      
      print("Old value:", str)  
      
      print("New value:", str2)

      Output:

      Old value: PYTHONAPP
      New value: pythonapp
      

      Python String Casefold() Method Example 2

      The strength of casefold, it not only converts into lowercase but also converts strictly. See an example below ‘β’ is converted into “ss”.

      # Python casefold() function example  
      
      # Variable declaration  
      
      str = "PYTHONAPP - β"  
      
      # Calling function  
      
      str2 = str.casefold()  
      
      # Displaying result  
      
      print("Old value:", str)  
      
      print("New value:", str2)

      Output:

      Old value: PYTHONAPP - β
      New value: pythonapp ? ss
      

      Python String Casefold() Method Example 3

      If string is in camelcase, it still converts whole string into lowercase.

      # Python casefold() function example  
      
      # Variable declaration  
      
      str = "PyThOnApP"  
      
      # Calling function  
      
      str2 = str.casefold()  
      
      # Displaying result  
      
      print("Old value:", str)  
      
      print("New value:", str2)

      Output:

      Old value: PyThOnApP
      New value: pythonapp
    1. Python String capitalize() Method

      In Python, the capitalize() method is used to convert the first character of the string into uppercase without altering the whole string. It changes the first character only and skips rest of the string unchanged.

      Python String capitalize() Method Syntax

      It has the following syntax:

      capitalize()  

      Parameters

      It does not required any parameter.

      Return Type

      It returns a modified string.

      Different Examples for Python String capitalize() Method

      Here, we are going to take several examples to demonstrate the working of the string capitalize() Method in Python.

      Python String capitalize() Method Example 1

      # Python capitalize() function example  
      
      # Variable declaration  
      
      str = "pythonapp"  
      
      # Calling function  
      
      str2 = str.capitalize()  
      
      # Displaying result  
      
      print("Old value:", str)  
      
      print("New value:", str2)

      Output:

      Old value: pythonapp
      New value: Pythonapp
      

      Python String Capitalize() Method Example 2

      What if first character already exist in uppercase.

      # Python capitalize() function example  
      
      # Variable declaration  
      
      str = "Pythonapp"  
      
      # Calling function  
      
      str2 = str.capitalize()  
      
      # Displaying result  
      
      print("Old value:", str)  
      
      print("New value:", str2)

      Output:

      Old value: Pythonapp
      New value: Pythonapp
      

      It returns the same string without any alteration.

      Python String Capitalize() Method Example 3

      What if first character is a digit or non-alphabet character? This method does not alter character if it is other than a string character.

      # Python capitalize() function example  
      
      # Variable declaration  
      
      str = "#pythonapp"  
      
      # Calling function  
      
      str2 = str.capitalize()  
      
      # Displaying result  
      
      print("Old value:", str)  
      
      print("New value:", str2)  
      
      print("--------digit---------")  
      
      str3 = "1-tpointtech"  
      
      str4 = str3.capitalize()  
      
      print("Old value:", str3)  
      
      print("New value:", str4)

      Output:

      Old value: #pythonapp
      New value: #pythonapp
      --------digit---------
      Old value: 1-pythonapp
      New value: 1-pythonapp