Signup/Sign In

How to convert a String to String array in Python

Posted in Technology   JULY 1, 2023

    The most commonly used object in any project and in any programming language is String only. In this tutorial, we will discuss how to convert string to string array using python programming language.

    First, let's discuss what is a string, array and how it differs from other programming languages.

    What is String?

    Any sequence of characters within either single quotes or double quotes is considered as a String.

    Syntax:

    s = 'Studytonight.com'
    
    s = "Studytonight.com"

    In most other languages like C, C++, Java, a single character within single quotes is treated as char data type value. But in Python we are not having char data type.

    Python doesn’t have char data type. Hence, it is treated as String only. For example:

    ch='a'
    
    type(ch)


    <class 'str'>

    What is array?

    In most other languages like C, C++, Java, array is a group of characters, and it is a variable which can store multiple values. But in the case of python, it has lists and other sequence types, which we can index like arrays.

    In a python, string can be considered as an array. We can create an array from an empty list by using append() method. Below is the example.

    Example:

    a=[]
    
    print(a)
    
    print(type(a))
    
    a.append("Python")
    
    a.append("is")
    
    a.append("very")
    
    a.append("easy")
    
    a.append("to")
    
    a.append("learn")
    
    print(a)
    
    print(type(a))


    []

    <class 'list'>

    ['Python', 'is', 'very', 'easy', 'to', 'learn']

    <class 'list'>

    How to convert string into list?

    There are many ways to conver string into string array/list in Python. Let's take a look at some of the most common ways to do that:

    1. By using list() built-in function.

    We can convert the string to list by using the built-in function list().

    Syntax:

    print(list(varable_name))

    Example:

    s="studytonight.com"
    
    print(type(s))
    
    print(list(s))
    
    print(type(s))


    <class 'str'>

    ['s', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't', '.', 'c', 'o', 'm']

    <class 'str'>

    1. By using split() method.

    Syntax:

    l=s.split(separator,maxsplit=1)

    Here, maxsplit is optional.

    • We can split the given string according to the specified separator by using split() method.
    • The default separator is space.

    • The return type of split() method is List.

    Example 1:

    s="study tonight"
    
    print(type(s))
    
    l=s.split()
    
    print(l)
    
    print(type(l))


    <class 'str'>

    ['study', 'tonight']

    <class 'list'>

    Example 2:

    s="www.studytonight.com"
    
    print(type(s))
    
    l=s.split(".")
    
    print(l)
    
    print(type(l))


    <class 'str'>

    ['www', 'studytonight', 'com']

    <class 'list'>

    In the above example,

    • In the first example, we consider the string “study tonight” to convert it to a list, which is stored in the variable s.

    • By default, split() method considers space as separator and returns list as output.

    • In the second example, we consider “www.studytonight.com” to convert it to a list.

    • As we are given “.” as a separator, the split() method splits the string according to the separator.

    1. By using rsplit() method.

    It is the same as the partition() method, but it works from the right side.

    Syntax:

    l=s.rsplit(separator,maxsplit=1)

    Again, maxsplit is optional.

    Example:

    s="www.studytonight.com"
    
    print(type(s))
    
    l=s.rsplit(".",1)
    
    print(l)
    
    print(type(l))


    ['www.studytonight', 'com']

    <class 'list'>

    How to convert string to tuple?

    A tuple is an immutable sequence of values that can be of different types which means an array/list which remains constant. Let’s see methods to convert string to tuple:

    1. partition() method

    Sytax:

    variable_name.partition(separator)

    Separator can also be string.

    • It is separated into three parts by using the given separator.

    • First part consists of the part before the specified separator.

    • The second part consists of the specified separator.

    • The third part consists of the part after the specified separator.

    • Returns tuple as output.

    Example 1: If we have a string “studytonight.com”

    s="studytonight.com"
    
    print(type(s))
    
    l=s.partition(".")
    
    print(l)
    
    print(type(l))


    <class 'str'>

    ('studytonight', '.', 'com')

    <class 'tuple'>

    • If the specified separator is not found, the partition() method returns a tuple.

    • It contains three parts,

    • First part will be the actual string.

    • The second part will be the empty string.

    • The third part will also be the empty string.

    Example 2:

    s="studytonight.com"
    
    print(type(s))
    
    l=s.partition("and")
    
    print(l)
    
    print(type(l))


    <class 'str'>

    ('studytonight.com', '', '')

    <class 'tuple'>

    Example3:

    s="study##tonight##com"
    
    print(type(s))
    
    l=s.partition("##")
    
    print(l)
    
    print(type(l))


    <class 'str'>

    ('study', '##', 'tonight##com')

    <class 'tuple'>

    In the above examples,

    • We consider studytonight.com as a string which is stored in variable s.

    • In the first example we applied the partition() method with a separator “.” on the string and it returns a tuple which consists of three parts.

    • In the second example we applied the partition() method with a separator “and” on the string, and it returns a tuple which consists of three parts, as the specified separator not found in the string, it returns with an empty string.

    • In the third example, as we can see, the separator is “##” which appears two times in the given string.

    • partition() method separates the string based on the first occurrence of the separator.

    • type() function is used to know the type of variable s and variable l after the partition() method is applied.

    1. rpartition() method

    Syntax:

    variable_name.rpartition(separator)

    Separator can also be string.

    It is the same as the partition() method, but it works from the right side.

    Example:

    s="study##tonight##com"
    
    print(type(s))
    
    l=s.rpartition("##")
    
    print(l)
    
    print(type(l))


    <class 'str'>

    ('study##tonight', '##', 'com')

    <class 'tuple'>

    This method split string into three parts based on the last occurrence of separator and returned a tuple with those parts.

    Author:
    I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.
    arraypythonstring
    IF YOU LIKE IT, THEN SHARE IT

    RELATED POSTS