Change Names

Posted on 2009-04-23
Last Modified: 2012-05-06

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:


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

Question by:dfernan
    LVL 28

    Accepted Solution

    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

    LVL 17

    Assisted Solution

    dfernan: Why did you specify "regex or sub"?

    Author Comment

    I guess I just wanted a regex general solution but yours is perfect so thanks a lot
    LVL 28

    Assisted Solution

    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).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    "The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
    Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now