Posted on Leave a comment

Python | Split String Variable Spaces

Rate this post

⭐Summary: The most efficient way to split a string using variable spaces is to use the split function like so given_string.split(). An alternate approach is to use different functions of the regex package to split the string at multiple whitespaces.

Minimal Example

text = "a b c d"
# Method 1
print(text.split())
# Method 2
import re
print(re.split('\s+', text))
# Method 3
print([x for x in re.findall(r'\S+', text) if x != ''])
# Method 4
print(re.sub(r'\s+', ',', text).split(','))
# Method 5
print(list(filter(None, text.split()))) # ['a', 'b', 'c', 'd']

Problem Formulation

📜Problem: Given a string. How will you split the string using multiple spaces?

Example

# Input
text = "abc xyz lmn pqr"
# Output
['abc', 'xyz', 'lmn', 'pqr']

The given input has multiple spaces between each substring, i.e., there are three spaces after abc, two spaces after xyz while a single space after lmn. So, not only do you have multiple spaces between the substring but also varied number of spaces. Can you split the string by varied and multiple spaces?


Though the question might look daunting at first but once you get hold of it, the solutions to this problem are easier than one can imagine. So, without further delay let us dive into the different ways of solving the given problem.

Method 1: Using split()

The built-in split('sep') function allows you to split a string in Python based on a given delimiter. By default the split function splits a given string at whitespaces. Meaning, if you do not pass any delimiter to the split function then the string will be split at whitespaces.

You can use this default property of the split function and successfully split the given string at multiple spaces just by using the split() function.

Code:

text = "abc xyz lmn pqr"
print(text.split()) # ['abc', 'xyz', 'lmn', 'pqr']

📚Recommended DigestPython String split()

Method 2: Using re.split

The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b'].

Approach: To split the string using multiple space characters use re.split("\s+", text) where \s+ is the matching pattern and it represents a special sequence that returns a match whenever it finds any whitespace character and splits the string. So, whenever there’s a space or multiple spaces (any number of occurrences of space are whitespace characters) the string will be split.

Code:

import re
text = "abc xyz lmn pqr"
print(re.split('\s+', text))
# ['abc', 'xyz', 'lmn', 'pqr']

📚Recommended Read:  Python Regex Split.

Method 3: Using re.findall

The re.findall(pattern, string) method scans the string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order when scanning the string from left to right.

📚Recommended Read: Python re.findall() – Everything You Need to Know

Code:

import re
text = "abc xyz lmn pqr"
print([x for x in re.findall(r'\S+', text) if x != ''])
# ['abc', 'xyz', 'lmn', 'pqr']

Method 4: Using re.sub

The regex function re.sub(P, R, S) replaces all occurrences of the pattern P with the replacement R in string S. It returns a new string. For example, if you call re.sub('a', 'b', 'aabb'), the result will be the new string 'bbbb' with all characters 'a' replaced by 'b'.

Approach: Use the re.sub method to replace all occurrences of space characters in the given string with a comma. Thus, the string will now have commas instead of space characters and you can simply split it using a normal string split method by passing comma as the delimiter.

Silly! Isn’t it? Nevertheless, it works.

Code:

import re
text = "abc xyz lmn pqr"
res = re.sub(r'\s+', ',', text).split(',')
print(res)
# ['abc', 'xyz', 'lmn', 'pqr']

Method 5: Using filter

Python’s built-in filter() function filters out the elements that pass a filtering condition. It takes two arguments: function and iterable. The function assigns a Boolean value to each element in the iterable to check whether the element will pass the filter or not. It returns an iterator with the elements that pass the filtering condition.

📚Related Read: Python filter()

Approach: You can use the filter() method to split the string by space. Feed in None as the first argument and the list of split strings as the second argument into the filter function. The filter() function then iterates through the list and filters out the spaces from the given string and returns only the non-whitespace characters. As the filter() method returns an object, we need to use the list() to convert the object into a list.

Code:

text = "abc xyz lmn pqr"
print(list(filter(None, text.split())))
# ['abc', 'xyz', 'lmn', 'pqr']

Conclusion

Hurrah! We have successfully solved the given problem using as many as five different ways. I hope you enjoyed reading this article and it helped you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!

Happy coding! 🙂

📚Suggested Read: Python Regex Superpower [Full Tutorial]


Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Posted on Leave a comment

Python | Split String Multiple Whitespaces

Rate this post

🍎Summary: The most efficient way to split a string using multiple whitespaces is to use the split function like so given_string.split(). An alternate approach is to use different functions of the regex package to split the string at multiple whitespaces.

Minimal Example:

import re text = "mouse\nsnake\teagle human"
# Method 1
print(text.split()) # Method 2
res = re.split("\s+", text)
print(res) # Method 3
res = re.sub(r'\s+', ',', text).split(',')
print(res) # Method 4
print(re.findall(r'\S+', text)) # ['mouse', 'snake', 'eagle', 'human']

Problem Formulation

📜Problem: Given a string. How will you split the string using multiple whitespaces?

Example

# Input
text = "abc\nlmn\tpqr xyz\rmno"
# Output
['abc', 'lmn', 'pqr', 'xyz', 'mno']

There are numerous ways of solving the given problem. So, without further ado, let us dive into the solutions.

Method 1: Using Regex

The best way to deal with multiple delimiters is to use the flexibility of the regular expressions library. There are different functions available in the regex library that you can use to split the given string. Let’s go through each one by one.

1.1 Using re.split

The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b'].

📚Recommended Read:  Python Regex Split.

Approach: To split the string using multiple whitespace characters use re.split("\s+", text) where \s is the matching pattern and it represents a special sequence that returns a match whenever it finds any whitespace character and splits the string.

Code:

import re
text = "abc\nlmn\tpqr xyz\rmno"
res = re.split("\s+", text)
print(res) # ['abc', 'lmn', 'pqr', 'xyz', 'mno']

1.2 Using re.findall

The re.findall(pattern, string) method scans the string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order when scanning the string from left to right.

📚Recommended Read: Python re.findall() – Everything You Need to Know

Code:

import re text = "abc\nlmn\tpqr xyz\rmno"
print(re.findall(r'\S+', text))

Explanation: In the expression, i.e., re.findall(r"\S'+", text), all occurrences of characters except whitespaces are found and stored in a list. Here, \S+ returns a match whenever the string contains one or more occurrences of normal characters (characters from a to Z, digits from 0-9, etc. However, not the whitespaces are considered).

1.3 Using re.sub

The regex function re.sub(P, R, S) replaces all occurrences of the pattern P with the replacement R in string S. It returns a new string. For example, if you call re.sub('a', 'b', 'aabb'), the result will be the new string 'bbbb' with all characters 'a' replaced by 'b'.

Aprroach: Use the re.sub method to replace all occurrences of whitespace characters in the given string with a comma. Thus, the string will now have commas instead of whitespace characters and you can simply split it using a normal string split method by passing comma as the delimiter.

Code:

import re
text = "abc\nlmn\tpqr xyz\rmno"
res = re.sub(r'\s+', ',', text).split(',')
print(res) # ['abc', 'lmn', 'pqr', 'xyz', 'mno']

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.


Method 2: Using split()

By default the split function splits a given string at whitespaces. Meaning, if you do not pass any delimiter to the split function then the string will be split at whitespaces. You can use this default property of the split function and successfully split the given string at multiple whitespaces just by using the split() function.

Code:

text = "abc\nlmn\tpqr xyz\rmno"
print(text.split())
# ['abc', 'lmn', 'pqr', 'xyz', 'mno']

📚Recommended Digest: Python String split()

Conclusion

We have successfully solved the given problem using different approaches. Simply using split could do the job for you. However, feel free to explore and try out the other options mentioned above. I hope this article helped you in your Python coding journey. Please subscribe and stay tuned for more interesting articles.

Happy Pythoning! 🐍 


Python Regex Course

Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.  

Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages

Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.  Regular expressions ​rule the game ​when text processing ​meets computer science. 

If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet:

Posted on Leave a comment

Python | Split String by Number

Rate this post

✨Summary: To split a string by a number, use the regex split method using the “\d” pattern.

Minimal Example

my_string = "#@1abc3$!*5xyz" # Method 1
import re res = re.split('\d+', my_string)
print(res) # Method 2
import re res = re.findall('\D+', my_string)
print(res) # Method 3
from itertools import groupby li = [''.join(g) for _, g in groupby(my_string, str.isdigit)]
res = [x for x in li if x.isdigit() == False]
print(res) # Method 4
res = []
for i in my_string: if i.isdigit() == True: my_string = my_string.replace(i, ",")
print(my_string.split(",")) # Outputs:
# ['#@', 'abc', '$!*', 'xyz']

Problem Formulation

📜Problem: Given a string containing different characters. How will you split the string whenever a number appears?

Method 1: re.split()

The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b'].

Code:

import re
my_string = "#@1abc3$!*5xyz"
res = re.split('\d+', my_string)
print(res) # ['#@', 'abc', '$!*', 'xyz']

Explanation: The \d special character matches any digit between 0 and 9. By using the maximal number of digits as a delimiter, you split along the digit-word boundary. 

Method 2: re.findall()

The re.findall(pattern, string) method scans string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order when scanning the string from left to right.

Code:

import re
my_string = "#@1abc3$!*5xyz"
res = re.findall('\D+', my_string)
print(res) # ['#@', 'abc', '$!*', 'xyz']

Explanation: The \special character matches all characters except any digit between 0 and 9. Thus, you are essentially finding all character groups that appear before the occurrence of a digit.

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Method 3: itertools.groupby()

Code:

from itertools import groupby
my_string = "#@1abc3$!*5xyz"
li = [''.join(g) for _, g in groupby(my_string, str.isdigit)]
res = [x for x in li if x.isdigit() == False]
print(res) # ['#@', 'abc', '$!*', 'xyz']

Explanation:

  • The itertools.groupby(iterable, key=None) function creates an iterator that returns tuples (key, group-iterator) grouped by each value of key. We use the str.isdigit() function as key function.
  • The str.isdigit() function returns True if the string consists only of numeric characters. Thus, you will have a list created by using numbers as separators. Note that this list will also contain the numbers as items within it.
  • In order to eliminate the numbers, use another list comprehension that checks if an element in the list returned previously is a digit or not with the help of the isdigit method. If it is a digit, the item will be discarded. Otherwise it will be stored in the list.

Method 4: Replace Using a for Loop

Approach: Use a for loop to iterate through the characters of the given string. Check if a character is a digit or not. As soon as a digit is found, replace that character/digit with a delimiter string ( we have used a comma here) with the help of the replace() method. This basically means that you are placing a particular character in the string whenever a number appears. Once all the digits are replaced by the separator string, split the string by passing the separator string as a delimiter to the split method.

Code:

my_string = "#@1abc3$!*5xyz"
res = []
for i in my_string: if i.isdigit(): my_string = my_string.replace(i, ",")
print(my_string.split(",")) # ['#@', 'abc', '$!*', 'xyz']

Conclusion

Phew! We have successfully solved the given problem and managed to do so using four different ways. I hope you found this article helpful and it answered your queries. Please subscribe and stay tuned for more solutions and tutorials.

Happy coding! 🙂

🌐Related Read: How to Split a String Between Numbers and Letters?

Posted on Leave a comment

How to Convert Octal String to Integer in Python

5/5 – (1 vote)

Problem Formulation

Given a string in the octal form:

s = '0o77'
# or s = '77'

How to convert the octal string to an integer in Python?

For example, you want to convert the octal string 'o10' to the decimal integer 8.

Here are a few other examples:

Octal String Decimal
'0o0' 0
'0o4' 4
'0o10' 8
'0o14' 12
'0o20' 16
'0o77' 63
'0o77777' 32767

Oct String to Integer using int() with Base 8

To convert an octal string to an integer, pass the string as a first argument into Python’s built-in int() function. Use base=8 as a second argument of the int() function to specify that the given string is an octal number. The int() function will then convert the octal string to an integer with base 10 and return the result.

Here’s a minimal example:

>>> int('0o77', base=8)
63

Examples

And here’s how you can convert the additional examples shown above:

>>> int('0o0', base=8)
0
>>> int('0o4', base=8)
4
>>> int('0o10', base=8)
8
>>> int('0o14', base=8)
12
>>> int('0o20', base=8)
16
>>> int('0o77', base=8)
63
>>> int('0o77777', base=8)
32767

You actually don’t need to use the prefix '0o' because your second argument already defines unambiguously that the given string is an octal number:

>>> int('0', base=8)
0
>>> int('4', base=8)
4
>>> int('10', base=8)
8
>>> int('14', base=8)
12
>>> int('20', base=8)
16
>>> int('77', base=8)
63
>>> int('77777', base=8)
32767

However, skipping the base but leaving the prefix raises a ValueError: invalid literal for int() with base 10: '0o77':

>>> int('0o77')
Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> int('0o77')
ValueError: invalid literal for int() with base 10: '0o77'

It assumes that the input string is in base 10 when in fact, it isn’t.

💡 Note: Even though passing a prefixed string '0o...' into the int() function is unambiguous, Python’s int() function doesn’t accept it if you don’t also define the base. This may be fixed in future versions!

In fact, you can specify the base argument as 0 to switch on base guessing—which should be the default behavior anyway! 👇

Base Guessing

You can pass a prefixed string '0o...' into the int() function and set the base to 0 to switch on base guessing in Python. This uses the prefix to determine the base automatically—without you needing to set it to 16. Yet, you still have to set it to 0 so the benefit is marginal in practice.

>>> int('0o7', base=8)
7
>>> int('0o7', base=0)
7
>>> int('0o7', 0)
7

Converting Octal Literals to Int

If you don’t have an octal string but a octal number—called a literal—such as 0xff, you don’t even need the int() function because Python will automatically convert it to a decimal number:

>>> 0o743
483
>>> 0o7
7
>>> 0o10
8

Background int()

Syntax: int(value [, base]) – > int
Argument value A Python object to be converted into an integer number. The value object must have an __int__() method that returns the associated integer number—otherwise a TypeError will be raised.
base An optional integer argument base to define the base of the numerical system in the value argument. If you set the base, the value argument must be a string. The base argument determines how the string argument is interpreted.
Return Value int Returns an integer number after converting the input argument value using its required __int__() method for the conversion.
YouTube Video

Do you still need more background information about Python’s built-in int() function? No problem, read over the related tutorial.

🌍 Related Tutorial: Python’s Built-in int() Function

Posted on Leave a comment

Python | Split String Hyphen

Rate this post

⭐Summary: Use "given string".split('-') to split the given string by hyphen and store each word as an individual item in a list. Some other ways to split using hyphen include using a list comprehension and the regex library.

Minimal Example

text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red"
# Method 1
print(text.split("-"))
# Method 2
import re
print(re.split('-', text))
# Method 3
print(list(filter(None, text.split('-'))))
# Method 4
print([x for x in re.findall(r'[^-]*|(?!-).*$', text) if x != '']) # OUTPUT: ['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']

Problem Formulation

📜Problem: Given a string, how will you split the string into a list of words using the hyphen as a delimiter?

Example

Let’s understand the problem with the help of an example.

# Input:
text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red"
# Output:
['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']

Now without any further ado, let’s dive into the numerous ways of solving this problem.

Method 1: Using split()

Python’s built-in split() function splits the string at a given separator and returns a split list of substrings. Here’s how the split() function works: 'finxterx42'.split('x') will split the string with the character ‘x’ as the delimiter and return the following list as an output: ['fin', 'ter', '42'].

Approach: To split a string by hyphen, you can simply pass the underscore as a separator to the split('-') function.

Code:

text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red"
print(text.split("-")) # ['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']

🌏Related Read: Python String split()

Method 2: Using re.split()

Another way of separating a string by using the underscore as a separator is to use the re.split() method from the regex library. The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b'].

Approach: You can use the re.split() method as re.split('-', text) where '-' returns a match whenever the string contains a hyphen. Whenever any hyphen is encountered, the text gets separated and the split substring gets stored as an element within the resultant list.

Code:

import re
text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red"
print(re.split('-', text)) # ['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']

🌏Related Read: Python Regex Split

Method 3: Using filter()

Note: This approach is efficient when the resultant list contains empty strings along with substrings.

Python’s built-in filter() function filters out the elements that pass a filtering condition. It takes two arguments: function and iterable. The function assigns a Boolean value to each element in the iterable to check whether the element will pass the filter or not. It returns an iterator with the elements that passes the filtering condition.

Approach: Use the filter() method to split the string by hyphen. The function takes None as the first argument and the list of split strings as the second argument. The filter() function then iterates through the list and removes any empty elements. As the filter() method returns an object, we need to use the list() to convert the object into a list.

Code:

text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red" print(list(filter(None, text.split('-')))) # ['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']

🌏Related Read: Python filter()

Method 4: Using re.findall()

The re.findall(pattern, string) method scans the string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order- when scanning the string from left to right.

Approach: You can use the re.findall() method from the regex module to split the string by hyphen. Use ‘[^-]|(?!-).$‘ as the pattern that can be fed into the findall function to solve the problem. It simply, means a set all characters that are joined by a hyphen will be grouped together.

Code:

import re
text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red" print([x for x in re.findall(r'[^_]*', text) if x != '']) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']

🌏Related Read: Python re.findall() – Everything You Need to Know

Python | Split String by Dot

Now that we have gone through numerous ways of solving the given problem, here’s a similar programming challenge for you to solve.

Challenge: You are given a string that contains dots in it. How will you split the string using a dot as a delimiter? Consider the code below and try to split the string by dot.

# Input:
text = "stars.moon.sun.sky" # Expected Output:
['stars', 'moon', 'sun', 'sky']

Try to solve the problem yourself before looking into the given solutions.

Solution: Here are the different methods to split a string by using the dot as a delimiter/separator:

text = "a*b*c"
# Method 1
print(text.split("*")) # Method 2
print(list(filter(None, text.split('*')))) # Method 3
import re
print([x for x in re.findall(r'[^/*]*|(?!/*).*$', text) if x != '']) # Method 4
print(re.split('[/*]', text))

Conclusion

Hurrah! We have successfully solved the given problem using as many as four different ways. We then went on to solve a similar coding challenge. I hope this article helped you. Please subscribe and stay tuned for more interesting articles!

Happy coding! 🙂


Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Posted on Leave a comment

Python | Split String by Underscore

Rate this post

⭐Summary: Use "given string".split() to split the given string by underscore and store each word as an individual item in a list.

Minimal Example

text = "Welcome_to_the_world_of_Python"
# Method 1
print(text.split("_")) # Method 2
import re
print(re.split('_', text)) # Method 3
print(list(filter(None, text.split('_')))) # Method 4
print([x for x in re.findall(r'[^_]*|(?!_).*$', text) if x != '']) # OUTPUT: ['Welcome', 'to', 'the', 'world', 'of', 'Python']

Problem Formulation

📜Problem: Given a string, how will you split the string into a list of words using the underscore as a delimiter?

Example

Let’s understand the problem with the help of an example.

# Input:
text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
# Output:
['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']

Now without any further ado, let’s dive into the numerous ways of solving this problem.

Method 1: Using split()

Python’s built-in split() function splits the string at a given separator and returns a split list of substrings. Here’s how the split() function works: 'finxterx42'.split('x') will split the string with the character ‘x’ as the delimiter and return the following list as an output: ['fin', 'ter', '42'].

Approach: To split a string by underscore, you need to use the underscore as the delimiter. You can simply pass the underscore as a separator to the split('_') function.

Code:

text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(text.split("_")) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']

🌏Related Read: Python String split()

Method 2: Using re.split()

Another way of separating a string by using the underscore as a separator is to use the re.split() method from the regex library. The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b'].

Approach: You can simply use the re.split() method as re.split('_', text) where '_' returns a match whenever the string contains an underscore. Whenever any underscore is encountered, the text gets separated at that point.

Code:

import re
text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(re.split('_', text)) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']

🌏Related Read: Python Regex Split

Method 3: Using filter()

Python’s built-in filter() function filters out the elements that pass a filtering condition. It takes two arguments: function and iterable. The function assigns a Boolean value to each element in the iterable to check whether the element will pass the filter or not. It returns an iterator with the elements that passes the filtering condition.

Approach: You can use the filter() method to split the string by underscore. The function takes None as the first argument and the list of split strings as the second argument. The filter() function iterates through the list and removes any empty elements. As the filter() method returns an object, we need to use the list() to convert the object into a list.

Code:

text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(list(filter(None, text.split('_')))) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']

🌏Related Read: Python filter()

Method 4: Using re.findall()

The re.findall(pattern, string) method scans the string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order- when scanning the string from left to right.

Approach: You can use the re.findall() method from the regex module to split the string by underscore. You can use ‘[^_]‘ as the pattern that can be fed into the findall function to solve the problem. It simply, means a set all characters that start with an underscore will be grouped together.

Code:

import re
text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print([x for x in re.findall(r'[^_]*', text) if x != '']) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']

🌏Related Read: Python re.findall() – Everything You Need to Know

Python | Split String by Dot

Now that we have gone through numerous ways of solving the given problem, here’s a similar programming challenge for you to solve.

Challenge: You are given a string that contains dots in it. How will you split the string using a dot as a delimiter? Consider the code below and try to split the string by dot.

# Input:
text = "stars.moon.sun.sky" # Expected Output:
['stars', 'moon', 'sun', 'sky']

Try to solve the problem yourself before looking into the given solutions.

Solution: Here are the different methods to split a string by using the dot as a delimiter/separator:

text = "stars.moon.sun.sky"
# Method 1
print(text.split(".")) # Method 2
print(list(filter(None, text.split('.')))) # Method 3
import re
print([x for x in re.findall(r'[^.]*|(?!.).*$', text) if x != '']) # Method 4
print(re.split('\\.', text))

Conclusion

Hurrah! We have successfully solved the given problem using as many as four different ways. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!


Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Posted on Leave a comment

Python Convert Hex to Base64

5/5 – (1 vote)

💬 Question: How to convert a hexadecimal string such as 02af01ff00 to a normal string using the Base64 format in Python?

👉 Short answer: Use the following fancy one-liner expression to convert the hex string s to a Base64-encoded Python string: base64.b64encode(bytes.fromhex(s)).decode().

For the long answer, keep reading! 🥸

If you’re like me, you may need a quick refresher on how Base64 encoding works and what it is exactly. Although I studied computer science a couple of years ago, I don’t have all those super basic “bits” of knowledge at the top of my head all the time.

You may already know about Base64 — in that case, I’d recommend you skip the next section and jump ahead right away. 👇

What Is Base64 Encoding?

Base64 is a very minimal encoding where a minimal set of characters — A-Z, a-z, and 0-9, essentially — are encoded using only six bits. Each bit position doubles the number of different encodings, so the Base64 encoding can encode 2*2*2*2*2*2 = 2^6 = 64 different characters.

Here’s the whole table — fortunately, the encoding is small and efficient enough that I can show you the whole thing! 🤯

(And, no, Emojis don’t have any place in Base64, it’s VERY old school!)

Index Binary Char
0 000000 A
1 000001 B
2 000010 C
3 000011 D
4 000100 E
5 000101 F
6 000110 G
7 000111 H
8 001000 I
9 001001 J
10 001010 K
11 001011 L
12 001100 M
13 001101 N
14 001110 O
15 001111 P
16 010000 Q
17 010001 R
18 010010 S
19 010011 T
20 010100 U
21 010101 V
22 010110 W
23 010111 X
24 011000 Y
25 011001 Z
26 011010 a
27 011011 b
28 011100 c
29 011101 d
30 011110 e
31 011111 f
32 100000 g
33 100001 h
34 100010 i
35 100011 j
36 100100 k
37 100101 l
38 100110 m
39 100111 n
40 101000 o
41 101001 p
42 101010 q
43 101011 r
44 101100 s
45 101101 t
46 101110 u
47 101111 v
48 110000 w
49 110001 x
50 110010 y
51 110011 z
52 110100 0
53 110101 1
54 110110 2
55 110111 3
56 111000 4
57 111001 5
58 111010 6
59 111011 7
60 111100 8
61 111101 9
62 111110 +
63 111111 /
Index Binary Character

🌍 Recommended Tutorial: Python Base64 – String Encoding and Decoding [+Video]

How to Convert Base64 Encoding (Hex String) to Human-Readable String in Python?

You can convert a hex string of the format '02af01ff00' to a Base64 encoded normal Python string by using the expression:

base64.b64encode(bytes.fromhex(s)).decode()

You can convert the resulting Base64 string back to a normal string by using the one-liner expression:

base64.b64decode(b64.encode()).hex()

Here’s a code example—I’ll break it down for you right after the code:

import base64 s = '02af01ff00' # hex string -> base64 string
b64 = base64.b64encode(bytes.fromhex(s)).decode() # base64 string -> hex string
s2 = base64.b64decode(b64.encode()).hex() print(s)
print(b64)
print(s2)

The output shows that you successfully converte from the hex string to the Base64 string and back to the hex string:

02af01ff00
Aq8B/wA=
02af01ff00

You can see that the start and end values of the conversion remain the same.

Let’s break down the code step by step!

Step 1: The initial hex string is still in a non-standardized format '02af01ff00'. We require it to be a bytes object because this is the required input format of the base64 functions shown in a moment. You use the bytes.fromhex() function.

>>> s = '02af01ff00'
>>> bytes.fromhex(s)
b'\x02\xaf\x01\xff\x00'

Step 2: You use the base64.b64encode() function to take the hex string (as bytes object) and convert it to a bytes object in Base64 encoding. This is almost what you want — but it’s not yet a normal Python string!

>>> base64.b64encode(bytes.fromhex(s))
b'Aq8B/wA='

Step 3: To convert the bytes object in Base64 encoding to a normal Python string, we use the bytes.decode() method.

>>> base64.b64encode(bytes.fromhex(s)).decode() 'Aq8B/wA='

Voilà, exactly what you wanted! But how to convert it back?

Step 4 and 5: You can convert the normal Base64-encoded Python string back to the hex string from the beginning by using the string.encode() method to obtain a bytes object, passing it into the base64.b64decode() function to obtain a Base64 bytes representation, and converting it to a hexadecimal string by using the bytes.hex() method.

>>> base64.b64decode('Aq8B/wA='.encode())
b'\x02\xaf\x01\xff\x00'
>>> base64.b64decode('Aq8B/wA='.encode()).hex() '02af01ff00'

Thanks ❤

Thanks for reading through the whole tutorial, I hope you managed to solve your issue! If not, you can check out this highly interesting SO answer.

Also, make sure to check out our free Python cheat sheets for maximal learning efficiency and fun!

Posted on Leave a comment

Python | Split String After Delimiter

Rate this post

Summary: You can use one of the following methods to split a string after the delimiter –

  • Using split
  • Using string slicing
  • Using regex
  • Using partition
  • Using removeprefix

Minimal Example

# Given String
chat = "Python Founder: Guido van Rossum"
# Method 1
print(chat.split(':')[1])
# Method 2
print(chat[chat.index(":")+1:])
# Method 3
import re
print(re.findall(":(.*)", chat)[0])
# Method 4
print(chat.partition(':')[2])
# Method 5
print(chat.removeprefix('Python Founder:'))

Problem Formulation

 📜Problem: Given a string; How will you split the string after the delimiter? The output must only contain the substring after the delimiter.

Example

Let’s visualize the problem with the help of an example:

# Input
text = "Subscribe to - Finxter"
# Expected Output
Finxter

⭐Method 1: Using split()

Approach: First, we will simply split thestring using “-” as the delimiter. Next, to extract the string before the delimiter we will use the index of the required substring. As the split() function returns a list of substrings, we can extract the last part using list indexing [1] (Indexing starts at 0).

Code:

text = "Subscribe to - Finxter"
res = text.split('-')[1]
print(res)
# Finxter

Note: The split() function splits the string at a given separator and returns a split list of substrings. It returns a list of the words in the string, using sep as the delimiter string.

🌎Related Read: Python String split()

⭐Method 2: Using String Slicing

Prerequisite: String slicing is a concept of carving out a substring from a given string. Use slicing notation s[start:stop:step] to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values.

Approach: First, we will use the index() method to find the occurrence of the delimiter in the text. Next, we will slice the string from the index next to the index of the delimiter until the end of the string. Note that we are starting from the index of the delimiter+1 because we don’t want to include it in the final output.

Code:

text = "Subscribe to - Finxter"
res = text[text.index("-")+1:]
print(res) # Finxter

Note:

The index()  method is used to return the index of the first occurrence of the specified substring, like find() but it raises a ValueError if the substring is not found.

🌎Related Reads:
String Slicing in Python

Python String index()

⭐Method 3: Using regex

The re.match(pattern, string) method returns a match object if the pattern matches at the beginning of the string. The match object contains useful information such as the matching groups and the matching positions.

Approach: Use the expression re.findall("-(.*)", given_string) to match and store all characters that come after the “-“.

Code:

import re text = "Subscribe to - Finxter"
print(re.findall("-(.*)", text)[0]) # Finxter

🌎Related Read: Python Regex Match

⭐Method 4: Using partition

The partition() method searches for a separator substring and returns a tuple with three strings: (1) everything before the separator, (2) the separator itself, and (3) everything after it. It then returns a tuple with the same three strings. 

Approach: We have used the partition method and used “-” as a separator. As we only need the substring before the delimiter, we have used the index of the required substring on the returned tuple and just printed the third element of the tuple (everything before the separator).

Code:

text = "Subscribe to - Finxter"
res = text.partition('-')[2]
print(res) # Finxter

🌎Related Read: Python String partition()

⭐Method 5: Using removeprefix

If you are using Python 3.9 or above then Python facilitates you with the removeprefix method that allows you to remove the substring that comes before a specified substring. Here’s a quick look at how the removeprefix method works –

source: https://docs.python.org/3.9/library/stdtypes.html#str.removeprefix

Code:

text = "Subscribe to - Finxter"
print(text.removeprefix('Subscribe to -')) # Finxter

Conclusion

Hurrah! We have successfully solved the given problem using as many as five different ways. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!


Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Posted on Leave a comment

3 Easy Habits That Can Make You Rich as a Freelance Coder

5/5 – (1 vote)

In this article, I’ll show you three simple habits to get way more done, have a much easier and stress-free life, and make more money. Almost guaranteed. Based on science!

But there’s a catch: these habits are often tough to implement for employees. I know because I have been there and done that.

When I finally created my own freelance-based business as a coder, all people in my environment were employees, and, unfortunately, nobody I knew implemented them. So I almost missed them! 😖

~~~

Here are the three daily habits that will increase the quality of your life and your success as a coder:

Habit #1 – Work from home

People think commute time is not a big deal.

In truth, commute time ranks among the most expensive things you can do and is highly detrimental to your financial success and your happiness.

Let’s start with happiness:

In a 2019 paper titled “How commuting affects subjective wellbeing”, researchers showed that long commute times reduce your happiness and can lead to mental issues like depression:

🎓 “It is found that longer commute times are associated with lower job and leisure time satisfaction, increased strain and poorer mental health.”

I could provide many more sources showing similar results, but let’s move on to the financial and time impact.

The average one-way commute time for Americans is 27.6 minutes, according to the Census Bureau.

In a year with 260 working days, that’s a whopping 240 hours of reduced productivity that is stressful and reduces your work energy.

For employees, you could argue it doesn’t matter. But as a self-employed freelancer, you could work 240 hours with an average freelance developer income of $50 that’s a $12,000 annual time opportunity cost.

Over a working life of only 30 years, contributing $6,000 to a passive index fund (ETF) at an average 10% annual yield would result in a whopping $1,136,118 nest egg.

You may miss out on becoming a millionaire just because of one hour per day in commute time!

Okay, let’s assume you’re stuck in the conventional mindset of believing your time is “free” and it doesn’t matter.

💬 Question: How much does the commute time actually cost in hard dollars without considering opportunity time costs and the drain of energy and happiness?

The average American commutes 41 miles a day to and from work (US Censor Bureau). 

The average cost per mile of driving a car is $0.58, according to the US Department of Energy.

So, the direct annual costs of commute time are 41 miles per day times $0.58 times 260 working days equals $6,182.

Thus, over 30 working years, here’s another missed $1.1 million USD nest egg if you’re the average American employee!

(See previous calculations.)

👉 Summary Commute: As an online freelance developer working from home, you’d easily pick up that $3.3 million USD over 30 years — just from saving commute time and investing the difference in the stock market.

And you’d be happier too!

I’d be missing out if I wouldn’t let you know about our current offer:

*** Become a Freelance Developer Course ***

But are there more simple gains you could add on top of those?

Yes, they exist! Let’s look at the second easy productivity habit that’s easy for freelancers but often tough to implement for employees!

Habit #3 – Have a 30-Minute Nap at Noon.

I sleep for half an hour every day at noon. My wife is from Spain, and it’s normal for my wife’s Spanish family to take a long siesta at noon.

Instead of plowing through a long 8-hour day until exhaustion, they work a couple of hours in the morning, sleep and recharge, and work for a couple of hours in the evening.

I love it – my quality of life has skyrocketed since I implemented this habit into my daily life.

And I became much more productive too.

Now you may ask: Is there scientific evidence on the benefits of daily afternoon naps?

You bet there is! In fact, there are too many studies to count. Here’s one:

In a popular MIT paper titled “The Economic Consequences of Increasing Sleep Among the Urban Poor,” researchers found that after more than three weeks of daily 30-minute naps, employees were more productive and invested more of their money into savings accounts.

Another paper titled “Effects of a Short Daytime Nap on the Cognitive Performance” demonstrated that napping in the afternoon improved all types of cognitive performance.

One study showed that the performance was improved by at least 2%, even considering the lost time through sleep. Although I believe taking a nap is way more beneficial than that if you’re a knowledge worker (=coder), let’s work with 2%.

2% of an average coding income of $100,000 USD yields an additional income of at least $2,000 per year. Put into the S&P 500, this yields an additional net worth of almost $400,000 USD after 30 years.

For sleeping…

Other results of similar studies are that people who take a daily nap are happier and healthier.

Not surprising results – but all the more powerful! And almost nobody is doing it!

Is it because today’s work environments have not yet caught up to the research? Maybe.

As a freelance developer, you can implement this simple habit in an instant — and make more money, be happier and healthier – just by sprinkling a little bit of sleep into your day!

Habit #3 – Go For a Morning Walk in Nature and Listen to Business Books.

There is strong scientific evidence for the benefits of daily walking, such as

🎓 “For every 2,000 steps, risk of heart disease, cancer, and premature death decreases by 10% […]” – with some convergence at the 10,000 steps per day level.

Also, walking in nature increases your work productivity and creativity:

🎓 “In 2014, two Stanford University researchers published some remarkable findings in a paper titled “Give your ideas some legs: the positive effect of walking on creative thinking”. […]

The results identified a clear link between walking and higher performance on the tests. In fact, compared to sitting, walking increased participants’ creative output by around 60 percent.”

Now, let’s layer in listening to business books on a daily basis while doing the walk.

From personal experience, I know that productivity goes through the roof as you keep compounding your business knowledge by reading business books.

It can easily increase your business results by double-digit percentage points every single year!

Let’s assume a mere 10% productivity increase from walking and listening to business books daily. (I think it’ll be 10 times that but let’s stay conservative).

As a freelance developer, your income will jump by $10,000 per year (or more). Over a 30-year life investing the difference in the S&P 500, yields a whopping $2,000,000 net worth.

And that’s not considering all the health benefits, reduced health costs, and increased life satisfaction when going for a walk daily.

By the way, here’s your first business audiobook, “Leaving the Rat Race with Python” (PDF + MP3). 100% free for you as a regular reader of this email academy!

💡 Note: This content originally came from an email sent to my coding email academy. You can signup here (free).

Summing It Up

If you implement habits (1), (2), and (3), that is, you work from home, take a nap daily, and go for a daily walk listening to business books, you can accumulate an additional fortune of $2.2M + $0.4M + $2M = $4.6M USD.

Now read again, and look how easy these habits are! Indeed, they are fun!

And your quality of life and health will improve – it may add years to your life expectancy! Low-hanging fruits, right? 

To your success! 🚀

One Final Note

Creating my own coding business has been the best decision in my career – it set me free in an instant.

If you want to learn the strategies to succeed as a business owner of a thriving coding business online, feel free to check out my Becoming a Freelance Developer course.

It’s fun and has already changed hundreds of students’ lives!

Posted on Leave a comment

Python | Split String Empty Separator

Rate this post

Summary: You can split a string using an empty separator using –
(i) list constructor
(ii) map+lambda
(iii) regex
(iv) list comprehension

Minimal Example:

text = '12345' # Using list()
print(list(text)) # Using map+lambda
print(list(map(lambda c: c, text))) # Using list comprehension
print([x for x in text]) # Using regex
import re
# Approach 1
print([x for x in re.split('', text) if x != ''])
# Approach 2
print(re.findall('.', text))

Problem Formulation

📜Problem: How to split a string using an empty string as a separator?

Example: Consider the following snippet –

a = 'abcd'
print(a.split(''))

Output:

Traceback (most recent call last): File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxter\Blogs\Finxter.py", line 2, in <module> a.split('')
ValueError: empty separator

Expected Output:

['a', 'b', 'c', 'd']

So, this essentially means that when you try to split a string by using an empty string as the separator, you will get a ValueError. Thus, your task is to find out how to eliminate this error and split the string in a way such that each character of the string is separately stored as an item in a list.


Now that we have a clear picture of the problem let us dive into the solutions to solve the problem.

Method 1: Use list()

Approach: Use the list() constructor and pass the given string as an argument within it as the input, which will split the string into separate characters.

Note: list() creates a new list object that contains items obtained by iterating over the input iterable. Since a string is an iterable formed by combining a group of characters, hence, iterating over it using the list constructor yields a single character at each iteration which represents individual items in the newly formed list.

Code:

a = 'abcd'
print(list(a)) # ['a', 'b', 'c', 'd']

🌎Related Read: Python list() — A Simple Guide with Video

Method 2: Use map() and lambda

Approach: Use the map() to execute a certain lambda function on the given string. All you need to do is to create a lambda function that simply returns the character passed to it as the input to the map object. That’s it! However, the map method will return a map object, so you must convert it to a list using the list() function.

Code:

a = 'abcd'
print(list(map(lambda c: c, a))) # ['a', 'b', 'c', 'd']

Method 3: Use a list comprehension

Approach: Use a list comprehension that returns a new list containing each character of the given string as individual items.

Code:

a = 'abcd'
print([x for x in a])
# ['a', 'b', 'c', 'd']

🌎Related Read: List Comprehension in Python — A Helpful Illustrated Guide

Method 4: Using regex

The re.findall(pattern, string) method scans string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order when scanning the string from left to right.

🌎Related Read: Python re.findall() – Everything You Need to Know

Approach: Use the regular expression re.findall('.',a) that finds all characters in the given string ‘a‘ and stires them in a list as individual items.

Code:

import re
a = 'abcd'
print(re.findall('.',a)) # ['a', 'b', 'c', 'd']

Alternatively, you can also use the split method of the regex library in a list comprehension which returns each character of the string and eliminates empty strings.

Code:

import re
a = 'abcd'
print([x for x in re.split('',a) if x!='']) # ['a', 'b', 'c', 'd']

🌎Related Read: Python Regex Split

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Conclusion

Hurrah! We have successfully solved the given problem using as many as four (five, to be honest) different ways. I hope this article helped you and answered your queries. Please subscribe and stay tuned for more interesting articles and solutions in the future.

Happy coding! 🙂


Regex Humor

Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)