Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Remove Everything After the Last Character in a String?

#1
How to Remove Everything After the Last Character in a String?

Problem Formulation


  • Given string s, and
  • character c.

How to remove all characters in s after the first occurrence of c?

Example


Given:
- string s = 'hello world', and - empty space character c = ' '. Desired result: 'hello'

Method 1: string.index() + slicing


To remove everything after the first occurrence of character c, determine the index of c in s with s.index©. Then pass the result as stop index into a slicing operation, starting from the first character such as in s[:s.index©]. The result is the substring up to the first occurrence of character c in s, excluded.

# Method 1: string.index() + slicing
s = 'hello world'
c = ' ' result = s[:s.index©] print(result)
# hello

Method 2: string.split()


The method str.split(sep, maxsplit) with maxsplit set to 1 splits the string into two substrings into a list. It splits the string at the first occurrence of the separator argument sep. By accessing the first element of this list, you obtain the string with everything before the first occurrence of the sep character.

# Method 2: string.split()
s = 'hello world'
c = ' ' result = s.split(c, 1)[0] print(result)
# hello

The string.split(delimiter, maxsplit=1) method creates a list of strings, split at the delimiter string. If you split at delimiter character c, the first element of the split list is the string before the first occurrence of character c. That’s why the operation s.split(c, 1)[0] gives you the string with everything removed after the first occurrence of the character c.

Method 3: Regex


The re.findall(pattern, string) method of Python’s regular expression library re creates a list of strings that match the pattern in the given string. By using the pattern '.*' + c, you match everything up to the first occurrence of the character c. You take the first element of the returned list with the indexing operation ...[0] and determine all characters but the last one (that is the matching character c) by using slicing ...[:-1] up to the second last element.

# Method 3: regex
import re
s = 'hello world'
c = ' ' result = re.findall('.*' + c, s)[0][:-1] print(result)
# hello

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post How to Remove Everything After the Last Character in a String? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/02/...-a-string/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How To Cut A String In Python? xSicKxBot 0 35 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] How to Split a String Between Numbers and Letters? xSicKxBot 0 31 01-29-2021, 04:27 AM
Last Post: xSicKxBot
  [Tut] Python Int to String with Leading Zeros xSicKxBot 0 63 11-17-2020, 03:22 AM
Last Post: xSicKxBot
  [Tut] How to Get MD5 of a String? A Python One-Liner xSicKxBot 0 84 11-07-2020, 03:58 AM
Last Post: xSicKxBot
  [Tut] How to Remove Duplicates From a Python List While Preserving Order? xSicKxBot 0 115 10-01-2020, 02:21 AM
Last Post: xSicKxBot
  [Tut] How To Format A String That Contains Curly Braces In Python? xSicKxBot 0 91 09-25-2020, 07:51 PM
Last Post: xSicKxBot
  [Tut] How to Access an Object Attribute Given the Attribute Name as a String? xSicKxBot 0 95 09-04-2020, 08:14 PM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Remove Multiple Items From a List xSicKxBot 0 185 06-29-2020, 04:37 AM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Convert a List to a String xSicKxBot 0 134 06-21-2020, 01:35 PM
Last Post: xSicKxBot
  [Tut] string.join(list) vs list.join(string) | Why Python’s Creators Chose The Former xSicKxBot 0 179 06-19-2020, 05:12 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
(Indie Deal) PAW Patrol, Ben10, Dead in ...
Last Post: xSicKxBot
Yesterday 11:22 PM
» Replies: 0
» Views: 0
News - You Can Now Play As Xenoblade’s P...
Last Post: xSicKxBot
Yesterday 11:21 PM
» Replies: 0
» Views: 0
News - A Quiet Place 2 Release Moves For...
Last Post: xSicKxBot
Yesterday 11:21 PM
» Replies: 0
» Views: 0
Mobile - Kick the Buddy guide – tips, tr...
Last Post: xSicKxBot
Yesterday 04:11 PM
» Replies: 0
» Views: 2
News - This Week At Bungie – 3/4/2021
Last Post: xSicKxBot
Yesterday 03:07 PM
» Replies: 0
» Views: 1
Xbox Wire - Horned Knight: Reintroducing...
Last Post: xSicKxBot
Yesterday 03:04 PM
» Replies: 0
» Views: 1
News - Which genre should your next PC g...
Last Post: xSicKxBot
Yesterday 03:03 PM
» Replies: 0
» Views: 5
News - One month in, Valheim has already...
Last Post: xSicKxBot
Yesterday 03:03 PM
» Replies: 0
» Views: 4
[1.07] BO4 Zombie Trainer (Semi-Patched)
Last Post: Appledryck
Yesterday 09:39 AM
» Replies: 13
» Views: 513
[1.06] BO4 Zombie Trainer (Call of duty ...
Last Post: Appledryck
Yesterday 09:37 AM
» Replies: 55
» Views: 2544

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020