Python Forum
How to change part of the value string in Python Dictionary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change part of the value string in Python Dictionary?
#7
(Feb-09-2020, 03:01 AM)sbabu Wrote: I appreciate if some one can advise me on how to train the brain to think certain way. Are there any brain exercises to improve my thinking?

One advice is keep practicing (mandatory for learning any language, whether spoken or programming). Second is use built in help (even if you understand very little of it at the beginning).

One possible scenario: you have string of numbers and need to manipulate it certain way - make all strings same length and padd with zeros if needed to achieve required length. '123' -> '000123', '123456' -> '123456'.

In interactive interpreter:

>>> s = '123'
What can I do with it? Lets have a look at built in methods available to s (which happens to be a str):

>>> s.       # 2 x TAB key
s.capitalize(   s.find(         s.isdecimal(    s.istitle(      s.partition(    s.rstrip(       s.translate(   
s.casefold(     s.format(       s.isdigit(      s.isupper(      s.replace(      s.split(        s.upper(       
s.center(       s.format_map(   s.isidentifier( s.join(         s.rfind(        s.splitlines(   s.zfill(       
s.count(        s.index(        s.islower(      s.ljust(        s.rindex(       s.startswith(  
s.encode(       s.isalnum(      s.isnumeric(    s.lower(        s.rjust(        s.strip(       
s.endswith(     s.isalpha(      s.isprintable(  s.lstrip(       s.rpartition(   s.swapcase(    
s.expandtabs(   s.isascii(      s.isspace(      s.maketrans(    s.rsplit(       s.title( 
I can scan all the names and pick most promising to find whether they are suitable for my purpose. Here I see zfill which may or not may mean 'zero fill'. How to find out?

>>> help(s.zfill)                         # or str.zfill
Help on built-in function zfill:

zfill(width, /) method of builtins.str instance
    Pad a numeric string with zeros on the left, to fill a field of the given width.
    
    The string is never truncated.
# press Q to close help and return to interactive interpreter
Seems something just about right. Lets try:

>>> s.zfill(6)
'000123'
>>> '123456'.zfill(6)
'123456'
>>> '1'.zfill(6)
'000001'
>>> ''.zfill(6)
'000000'
We have achieved our objective without leaving Python. You can check other string methods to find out what they do, same with other datatypes (lists, tuples, dictionaries etc). This way you don't need to reinvent wheel and can take advantage features which are already built in into language.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: How to change part of the value string in Python Dictionary? - by perfringo - Feb-09-2020, 08:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,724 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  struggling with one part of python Lucifer 23 6,600 May-08-2020, 12:24 PM
Last Post: pyzyx3qwerty
  From string parameter to a dictionary Mitchie87 9 3,217 Oct-12-2019, 10:34 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020