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 9191529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:42:21+00:00 2026-06-17T20:42:21+00:00

From an S.O answer : Don’t modify strings. Work with them as lists; turn

  • 0

From an S.O answer:

“Don’t modify strings.

Work with them as lists; turn them into strings only when needed.

… code sample …

Python strings are immutable (i.e. they can’t be modified). There are a lot of reasons for this. Use lists until you have no choice, only then turn them into strings.”

Is this considered best practice?

I find it a bit odd that Python has methods that return new modified strings (such as upper(), title(), replace() etc.) but doesn’t have an insert method that returns a new string. Have I missed such a method?

Edit: I’m trying to rename files by inserting a character:

import os
for i in os.listdir('.'):
    i.insert(3, '_')

Which doesn’t work due to immutability. Adding to the beginning of a string works fine though:

for i in os.listdir('.'):
    os.rename(i, 'some_random_string' + i)

Edit2: the solution:

>>> for i in os.listdir('.'):                                                │··
...  os.rename(i, i[:4] + '_' + i[4:])

Slicing certainly is nice and solves my problem, but is there a logical explanation why there is no insert() method that returns a new string?

Thanks for the help.

  • 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-17T20:42:23+00:00Added an answer on June 17, 2026 at 8:42 pm

    If you want to insert at a particular spot, you can use slices and +. For example:

    a = "hello"
    b = a[:2] + '_S1M0N_' + a[2:]
    

    then b will be equal to he_S1M0N_llo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From dahlbyk answer to this question : Map two lists into a dictionary in
In my answer from yesterday I called the following piece of code a hack:
I am using the accepted answer from this question . The relevant code is
Of course, I know the best answer is don't write your own cross-platform code,
I copied the code from the answer here and I still am getting a
I am trying to run the code from the answer at the UIView vertical
The code below is adapted from this answer function MessageClass() { var self =
From another answer on Stackoverflow is a conversion from Javascript date to .net DateTime:
From this answer: When is a C++ terminate handler the Right Thing(TM)? It would
Based on an answer from a candidate I have a confusion regarding the functioning

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.