Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:37:47+00:00 2026-06-11T23:37:47+00:00

i am trying to delete certain portion of a string if a match found

  • 0

i am trying to delete certain portion of a string if a match found in the string as below

string = 'Newyork, NY'

I want to delete all the characters after the comma from the string including comma, if comma is present in the string

Can anyone let me now how to do this .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:37:48+00:00Added an answer on June 11, 2026 at 11:37 pm

    Use .split():

    string = string.split(',', 1)[0]
    

    We split the string on the comma once, to save python the work of splitting on more commas.

    Alternatively, you can use .partition():

    string = string.partition(',')[0]
    

    Demo:

    >>> 'Newyork, NY'.split(',', 1)[0]
    'Newyork'
    >>> 'Newyork, NY'.partition(',')[0]
    'Newyork'
    

    .partition() is the faster method:

    >>> import timeit
    >>> timeit.timeit("'one, two'.split(',', 1)[0]")
    0.52929401397705078
    >>> timeit.timeit("'one, two'.partition(',')[0]")
    0.26499605178833008
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to delete a certain field from one table and I want
I'm trying to delete certain lines from a file. My file is a .txt
I am trying to delete certain key/value pairs from a hash, but I get
I'm trying to find a way to delete the first match of a certain
I'm trying to delete all tables from a database except one, and I end
I am trying to make a schedule table that would delete certain rows according
I am trying to delete the clientside session cookie when I access a certain
I am trying to delete MySQL table rows from a Web page using Perl.
I'm trying to delete a specific line by id from a file in C++
I'm trying to delete all rows in a table using Castle ActiveRecord. Normally, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.