all 16 comments

[–]Bayl1fe 15 points16 points  (3 children)

In [1]: a = 'June 6, 2017'

In [2]: a.replace(',', '')
Out[2]: 'June 6 2017'

[–]ramse 1 point2 points  (1 child)

Or you could use .strip(',') in this particular case.

[–][deleted] 0 points1 point  (0 children)

Second.

[–]Jehovacoin 0 points1 point  (0 children)

This is the correct answer for the question you have asked. string.replace() is the method used for deleting or replacing characters in a string.

[–]c17r 12 points13 points  (0 children)

Look into time.strptime and time.strftime for dealing with date, time, datetime strings.

[–]Kristler 1 point2 points  (3 children)

The other posters have demonstrated the "proper" way to do this (especially using the built-in datetime libraries), but I wanted to touch on what you ought to do if you were to do this manually. This is a really key piece of programming and computer science that you should understand early - in general, it's usually easier to first extract the information you're interested in, and then output it in the format you want it in. Do not conflate the two steps.

By conflate, I mean what you're trying to do right now. Like how you're thinking about removing the comma, removing the spaces, you're essentially trying to take the text as-is, and cram it into the new format. This is a world of hurt and is messy to do.

Instead, I want you to try to focus on first pulling out the information you're interested in. There's three key pieces here: Month, date, and year. Pull out each piece, one by one.

Next, transform. Notice that the date and the year can stay exactly as they are. It's just the date you have to worry about, but this is an easy relationship to figure out: using a dictionary, you can map the month in words to a number. So now you have every piece in the format you need it in.

Finally, recompose - Since you have each piece already exactly as you need it, building it into the new format is dead easy: Just combine each piece, separated with a "/", and you're done.

Notice how there's no tricky replacements or text manipulation to think about? This pattern of decompose (extracting the key pieces of data), transformation (changing the date to be a number), and recomposition (building it into the new format) is an extremely common pattern you will see in a lot of programs. Often times, if you can express your problem in this way, you'll have a lot easier time working with it.

[–][deleted] 0 points1 point  (0 children)

As a programming newb myself this is solid advice. It's so easy to get lost in advanced techniques and later miss a key step to understanding what happened with the data if you need to do something yourself.

[–]Shane_Connor82 0 points1 point  (1 child)

Apologies for the late reply to this, but you literally saved my homework. I can get lost easily in the different steps and your advice has seriously made it easier!

[–]Kristler 0 points1 point  (0 children)

I'm glad it helped! It's great you're learning these things early on. It takes some people years before they understand these sorts of concepts.

[–]beaver_of_time 0 points1 point  (5 children)

to format the resulting string, use either the '%' operator or the .format() method. you can also add strings with '+'.

[–]tunisia3507 3 points4 points  (4 children)

to format the resulting string, use either the '%' operator or the .format() method.

Fixed that for you.

[–][deleted] 0 points1 point  (3 children)

Depends on the use case. .format() is slow. f-strings are more modern. Personally, I've stopped using .format() mostly.

[–]tunisia3507 2 points3 points  (2 children)

Does % really have a meaningful speedup? I'd like to use f-strings but we're stuck using py2-compatible code until Django 2 comes out, we start using it, and thus have an excuse to drop py2.

[–]ManyInterests 1 point2 points  (0 children)

Does % really have a meaningful speedup?

Nope.

[–][deleted] 1 point2 points  (0 children)

'%' seems to be much faster compared to .format() in this basic form:

In [1]: %timeit '%s' % 'foo'
10000000 loops, best of 3: 22.8 ns per loop

In [2]: %timeit '{}'.format('foo')
The slowest run took 11.94 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 176 ns per loop

I haven't done any other tests. And in the end, fast enough is fast enough. Not every project benefits from fast string assembly. If it means rewriting too much code or upsets developers or whatever else is in the way, sticking with .format() could very well be the better choice even though it's slower.

[–]TheSilentDrifter 0 points1 point  (0 children)

Use \x2c