• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Change Names

Hi,

I want to change the last C to a c in the names that have a c at the end. Any suggestions on how to do that in python?

My data is of the type:

SPAC1834.03C
SPAC821.09
SPBC3E7.12C
SPAC19G12.06C
:
:
etc....

Goal: change the last C to a c using regex or sub...


0
dfernan
Asked:
dfernan
  • 2
3 Solutions
 
peprCommented:
The name[:-1] returns the name without the last character. Then you append anything else instead of it. You cannot modify the string by simply assigning another character to a position (in Python). This is because the string is immutable (cannot be changed).
names = ['SPAC1834.03C', 'SPAC821.09', 'SPBC3E7.12C', 'SPAC19G12.06C']
         
for name in names:
    if name.endswith('C'):
        name = name[:-1] + 'c'
        print name         

Open in new window

0
 
ramromconsultant Commented:
dfernan: Why did you specify "regex or sub"?
0
 
dfernanAuthor Commented:
I guess I just wanted a regex general solution but yours is perfect so thanks a lot
0
 
peprCommented:
Generally, it is better (often also more efficient) to solve the simple problems without regular expressions. This does not mean "always without regular expressions". Sometimes "a bit more of source text" is more understandable, less error prone, and faster (both to write and to run).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now