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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:17:28+00:00 2026-05-30T11:17:28+00:00

What is the best / correct way to use item assignment for python string

  • 0

What is the best / correct way to use item assignment for python string ?

i.e s = "ABCDEFGH" s[1] = 'a' s[-1]='b' ?

Normal way will throw : 'str' object does not support item assignment

  • 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-05-30T11:17:29+00:00Added an answer on May 30, 2026 at 11:17 am

    Strings are immutable. That means you can’t assign to them at all. You could use formatting:

    >>> s = 'abc{0}efg'.format('d')
    >>> s
    'abcdefg'
    

    Or concatenation:

    >>> s = 'abc' + 'd' + 'efg'
    >>> s
    'abcdefg'
    

    Or replacement (thanks Odomontois for reminding me):

    >>> s = 'abc0efg'
    >>> s.replace('0', 'd')
    'abcdefg'
    

    But keep in mind that all of these methods create copies of the string, rather than modifying it in-place. If you want in-place modification, you could use a bytearray — though that will only work for plain ascii strings, as alexis points out.

    >>> b = bytearray('abc0efg')
    >>> b[3] = 'd'
    >>> b
    bytearray(b'abcdefg')
    

    Or you could create a list of characters and manipulate that. This is probably the most efficient and correct way to do frequent, large-scale string manipulation:

    >>> l = list('abc0efg')
    >>> l[3] = 'd'
    >>> l
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    >>> ''.join(l)
    'abcdefg'
    

    And consider the re module for more complex operations.

    String formatting and list manipulation are the two methods that are most likely to be correct and efficient IMO — string formatting when only a few insertions are required, and list manipulation when you need to frequently update your string.

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

Sidebar

Related Questions

I wondered about the best way to implement a correct, flexible and fast Equals
What is the best way to check if a user has correct rights to
I've got a quick question regarding the use of repositories. But the best way
I'm still trying to get my head around the correct way to use Azure
I'd like to know the correct / best way to handle concurrency with an
I'm wondering what the correct way to compare two characters ignoring case that will
I'm trying to use console.log(item) to find out if my selector is correct, what
I'm posting this question because I do not know the best/correct way of doing
I'm having trouble finding examples of the correct way to use NSError , UIAlertView
What is the correct/best way of handling spaces and quotes in bash completion? Here’s

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.