What is ‘String'?

Everything is an Object in Python, hence even String is treated as an object in Python.

The sequence of characters is called String. A character can be anything like symbols, alphabets, numbers etc. The computer doesn’t understand any of these characters or Strings, rather it understands only binary numbers i.e. 0’s and 1’s.

We call this method as encoding and the reverse process is called decoding, and encoding is done based on the ASCII.

Declaring a String

Strings are declared using double quotes (“ “) or single quotes (‘ ‘).

Syntax:

Variable name = “string value”

OR

Variable name = ‘string value’

Example 1:

my_string = “Hello”

Example 2:

my_string = ‘Python’

Example 3:

my_string = “Hello World”
print(“String is: “, my_string)

Output:

String is: Hello World

Example 4:

my_string = ‘Hello Python’
print(“String is: “, my_string)

Output:

String is: Hello Python

What is String Split?

As the name itself explains String split means splitting or breaking the given String into smaller pieces.

If you would have worked on Strings in any programming languages, then you might know about concatenation (combining the strings) and String split is just the opposite of it. In order to perform split operations on strings, Python provides us with a built-in function called split().

Python Split function

Python split() method is used to split the string into chunks, and it accepts one argument called separator.

A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.

Syntax:

variable_name = “String value”
variable_name.split()

Example 1:

my_string = “Welcome to Python”
my_string.split()

Output:

[‘Welcome’, ‘to’, ‘Python’]

How to Split a String in Python?

In the above example, we have used the split() function to split the string without any arguments.

Let’s see some examples of splitting the string by passing some arguments.

In the above example, we are splitting the given string “Apple, Orange, Mango” into three parts and assigning these three parts into different variables fruit1, fruit2 and fruit3 respectively.

Split String into List

Whenever we split the string in Python, it will always be converted into List.

As you know, we don’t define any data types in Python, unlike other programming languages. Hence, whenever we use the split() function it’s better that we assign it to some variable so that it can be accessed easily one by one using the advanced for loop.

Example 1:

my_string = “Apple,Orange,Mango”
value = my_string.split(‘,’)

for the item in value:

print(item)

Output:

Apple Orange Mango

Split String into Array

As we discussed earlier, whenever we split the string it will always be converted into an Array. However, the way you access data will differ.

Using the split() function, we break the string into some pieces and assign it to some variable, hence using the index we can access the broken strings and this concept is called Arrays.