Warning: session_start(): open(/tmp/sess_b14f51211396a1c0657b47c66d9959ee, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Split A String Into A List Of Characters In Python - LearnShareIT

How To Split A String Into A List Of Characters In Python

Split a string into a list of characters in python

With this guide, we will learn how to split a string into a list of characters in Python. Python is a relatively compact language in terms of coding syntax and also supports many methods and libraries to make it easier for programmers to use. So, here are some ways that you can apply to do this.

Split a string into a list of characters in python

Method 1: Using the unpack(*) method

Unpack can only be applied to an object of type iterable. To apply, you need to put the * symbol right in front of that object.

Syntax:

[*[String]]

Parameters:

  • String: String you want to split.

Below is the code for this example:

myStr = 'LearnShareIT'
print([*myStr])

Unpack helps to separate the string into a list of independent characters, so the Output is a set of characters extracted from the original string.

Output:

['L', 'e', 'a', 'r', 'n', 'S', 'h', 'a', 'r', 'e', 'I', 'T']

A string that we want to split into a completed List of characters as we want.

Method 2: Using list comprehension

With list-comprehension, we can do the above work with just one line of code, and it’s very easy to understand:

Syntax: 

[x for x in String]

Parameters:

  • String: string you want to split.

See the example below to understand better:

myStr = "LearnShareIT"
newList = [x for x in myStr]

print(newList)

With this method, your string will be split into separate characters

Output:

['L', 'e', 'a', 'r', 'n', 'S', 'h', 'a', 'r', 'e', 'I', 'T']

Method 3: Using the extend() method

The extend() in Python is a method used to add elements of an iterable (an object containing many elements) such as a list, tuple, or string to a list.

Syntax: 

List.extend(string)

Parameters:

  • string: string you want to split.
  • List: an empty array.

Everything is almost the same as in method 2, and only the primary difference is in syntax. Look at the code below to see that difference.

myStr = 'LearnShareIT'
mylist = []

mylist.extend(myStr)
print(mylist)

Using an empty array, we can split the string as follows.

Output:

['L', 'e', 'a', 'r', 'n', 'S', 'h', 'a', 'r', 'e', 'I', 'T']

Summary

There are many ways to split a string into a list of characters in Python, with the help of methods and libraries. The above are some of the ways that I think are the most concise and easy to understand that you can apply during your work.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *