![]() |
|
[Tut] Python | Split String and Get Last Element - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python | Split String and Get Last Element (/thread-100198.html) |
[Tut] Python | Split String and Get Last Element - xSicKxBot - 11-07-2022 Python | Split String and Get Last Element Rate this post Summary: Use Code: given_string.rsplit('sep', 1)[-1]Code: given_string.rpartition('sep', 1)[-1]Minimal Solution: Code: dob = '21/08/2023'
# Method 1
print(dob.rsplit('/', 1)[-1])
# Method 2
print(dob.rpartition('/')[-1]) # OUTPUT: 2023[b]Problem Formulation[/b] Problem: Given a string. How will you split the string and get the last element?Let’s try to understand the given problem with the help of an example: Example 1 Code: # Input:
text = 'Java_C++_C#_Golang_Python'
# Expected Output:
Split String: ['Java_C++_C#_Golang', 'Python']
Last Element: PythonIn the above example, “ Code: _Example 2 Code: # Input:
text = 'Java_C++_C#_Golang_Python_'
# Expected Output:
Split String: ['Java_C++_C#_Golang', 'Python']
Last Element: PythonUnlike the previous example, the input string ends with the separator itself. However, the output is similar. So, you have a different input string, but you have to generate a similar output by eliminating the separator. Let’s dive into the different ways of solving the given problems. Method 1: Using rsplit Prerequisite: Simply put, the Code: rsplitCode: finxterx42'.rsplit('x')Code: ['fin', 'ter', '42']Code: rsplit
Thus, you can use the Code: maxsplitCode: maxsplit = 1Code: rsplitSolution to Example 1: Code: text = 'Java_C++_C#_Golang_Python'
print("Split String: ", text.rsplit('_', 1))
print("Last Element: ", text.rsplit('_', 1)[-1])Output: Code: Split String: ['Java_C++_C#_Golang', 'Python']
Last Element: PythonSolution to Example 2: In the second scenario, you must get rid of the separator that comes at the end of the string. Otherwise, simply using Code: rsplitCode: maxsplit![]() To avoid this problem, you can use the strip() function to get rid of the separator and then use Code: rsplitCode: text = 'Java_C++_C#_Golang_Python_'
text = text.strip('_')
print("Split String: ", text.rsplit('_', 1))
print("Last Element: ", text.rsplit('_', 1)[-1])Output: Code: Split String: ['Java_C++_C#_Golang', 'Python'] Last Element: PythonYou can also use the Code: rpartitionCode: rpartitionCode: finxterx42'.rpartition('x')Code: ('finxter', 'x', '42')Thus, you can simply extract the last item from the tuple after the string has been cut by the Code: rpartitionCode: Code: # Solution to Example 1
text = 'Java_C++_C#_Golang_Python'
print("Split String: ", text.rpartition('_'))
print("Last Element: ", text.rpartition('_')[-1]) # Solution to Example 2
text = 'Java_C++_C#_Golang_Python_'
text = text.strip('_')
print("Split String: ", text.rpartition('_'))
print("Last Element: ", text.rpartition('_')[-1])Output: Code: Split String: ('Java_C++_C#_Golang', '_', 'Python')
Last Element: Python [b]Coding Challenge[/b]Before we wrap up this tutorial, here’s a coding challenge for you to test your grip on the concept you just learned. Input: Consider the following IP Address – ip = 110.210.130.140 Challenge: Extract the network bit from the given class A ip address and convert it to its binary form. Expected Output: 10001100 Hint:– 140 is the network bit! – How to Convert a String to Binary in Python? Solution: Code: ip = '110.210.130.140'
nw_bit = int(ip.rpartition('.')[-1])
print(bin(nw_bit)[2:])Explanation: The solution is pretty straightforward. You first have to extract the network bit, i.e., 140. This happens to be the last item after the “ Code: .Code: rpartitionCode: rpartitionCode: bin Related Read: Python Print Binary Without ‘0b’[b]Conclusion[/b] I hope you enjoyed the numerous scenarios and challenges used in this tutorial to help you learn the two different ways of splitting a string and getting the last item. Please subscribe and stay tuned for more interesting tutorials and solutions. [b]Recommended Reads: ⦿ How To Split A String And Keep The Separators?⦿ How To Cut A String In Python?⦿ Python | Split String into Characters[/b] |