Cara menggunakan split binary string python

Tipe Data Python – Bytes, mirip dengan string. Bytes adalah immutable sequence of bytes. Type bytes baik digunakan untuk raw binary data maupun ascii character. Binary data umum digunakan saat misalnya mengakses data HTTP request.

Cara mendeklarasikan bytes adalah b”nilai variable yang kita inginkan” atau dapat menggunakan single quote

>>> d = b"data bytes"

Pada Type data bytes kita tetap dapat menggunakan method dari str. Contoh kita menggunakan method split pada data bytes. Hasilnya adalah berupa list byte object.

>>> d.split()
[b'data', b'bytes']

Kita juga dapat mengkonversi dari byte ke string dan sebaliknya. Yang perlu kita ketahui adalah encoding dari byte sequence tersebut.

Masalah encoding ini mungkin terlihat tidak penting pada awal-awal, namun perlu diperhatikan saat kita bekerja dengan files, network resources, HTTP request ditransmisikan dalam mode bytes. Sementara kita lebih leluasa bekerja di unicode strings

At times while working in our programs, we may get a situation where we want to break a string into smaller parts for further processing.

In this tutorial, we will take an in-depth look at String split in Python with simple examples for your easy understanding.

=> Visit Here To Learn Python From Scratch

Cara menggunakan split binary string python

What You Will Learn:

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.

Example 1:

my_string = “Apple,Orange,Mango”
print(“Before splitting, the String is: “, my_string)
value = my_string.split(‘,’)
print(“After splitting, the String is: “, value)

Output:

Before the split, the String is: Apple, Orange, Mango
After split, the String is: [‘Apple’, ‘Orange’, ‘Mango’]

Example 2:

my_string = “Welcome0To0Python”
print(“Before splitting, the String is: “, my_string)
value = my_string.split(‘0’)
print(“After splitting, the String is: “, value)

Output:

Before splitting, the String is: Welcome0To0Python
After splitting, the String is: [‘Welcome’, ‘To’, ‘Python’]

Example 3:

Variable name = ‘string value’
0

Output:

First Fruit is: Apple
Second Fruit is: Orange
Third Fruit is: Mango

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:

Variable name = ‘string value’
1

for the item in value:

Variable name = ‘string value’
2

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.

Let’s see how we can access the split data using arrays.

Example 1:

Variable name = ‘string value’
3

Output:

First item is: Apple
Second item is: Orange
Third item is: Mango

Tokenize String

When we split the string, it breaks down into smaller pieces and these smaller pieces are called tokens.

Example:

Variable name = ‘string value’
4

Output:

String tokens are: [‘Audi’, ‘BMW’, ‘Ferrari’]

In the above example Audi, BMW, and Ferrari are called the tokens of string.

“Audi,BMW,Ferrari”

Split String by Character

In Python, we have an in-built method called list() to split the strings into a sequence of characters.