How do you replace a character in a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pythondummy
    New Member
    • Apr 2010
    • 3

    #1

    How do you replace a character in a string?

    just wondering how to replace a space (' ') in a string to a dot ('.')

    so far i have this...

    Code:
    def add_dots(game):
           for row in game:
            for char in row:
                if char == ' ':
    thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Since strings are immutable, you must create a new string. Following are a couple of ways:
    Code:
    >>> s = 'abcdef ghijk'
    >>> s1 = s.replace(' ', '.')
    >>> s1
    'abcdef.ghijk'
    >>> s2 = ''.join([[c,'.'][c == ' ' or 0] for c in s])
    >>> s2
    'abcdef.ghijk'
    >>>

    Comment

    • pythondummy
      New Member
      • Apr 2010
      • 3

      #3
      Originally posted by bvdet
      Since strings are immutable, you must create a new string. Following are a couple of ways:
      Code:
      >>> s = 'abcdef ghijk'
      >>> s1 = s.replace(' ', '.')
      >>> s1
      'abcdef.ghijk'
      >>> s2 = ''.join([[c,'.'][c == ' ' or 0] for c in s])
      >>> s2
      'abcdef.ghijk'
      >>>
      hey, thanks for that. However i am using python IDLE... i can't use '.replace' i have to use range or enumerate....

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        The last time I checked, str.replace() worked in Idle. Did you notice the list comprehension in my previous post? You could do something like that.

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          Originally posted by pythondummy
          just wondering how to replace a space (' ') in a string to a dot ('.')

          so far i have this...

          Code:
          def add_dots(game):
                 for row in game:
                  for char in row:
                      if char == ' ':
          thanks
          So obviously the ways that @bvdet suggested are superior, but if you really want to do it the way you suggested, then do the following (by the way, the indentation in your code was wrong - you need to be careful with python! The standard is 4 spaces for a tab, but any number is okay as long as it's consistent)

          Code:
          def add_dots(game):
              newgame=[]
              for row in game:
                  newrow=""
                  for char in row:
                      if char == ' ':
                          newrow+="."
                      else:
                          newrow+=char
                  newgame.append(newrow)
              return newgame
          I haven't tried it, so I'll leave it as an exercise to debug. If you're still stuck, just post back.

          We'd also appreciate it if you post back how you've done good or bad!

          Comment

          • pythondummy
            New Member
            • Apr 2010
            • 3

            #6
            Originally posted by Glenton
            So obviously the ways that @bvdet suggested are superior, but if you really want to do it the way you suggested, then do the following (by the way, the indentation in your code was wrong - you need to be careful with python! The standard is 4 spaces for a tab, but any number is okay as long as it's consistent)

            Code:
            def add_dots(game):
                newgame=[]
                for row in game:
                    newrow=""
                    for char in row:
                        if char == ' ':
                            newrow+="."
                        else:
                            newrow+=char
                    newgame.append(newrow)
                return newgame
            I haven't tried it, so I'll leave it as an exercise to debug. If you're still stuck, just post back.

            We'd also appreciate it if you post back how you've done good or bad!
            haha, all good... i figured it out finally. i used enumerate

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Here's a couple of ways using a for loop:
              Code:
              >>> s
              'abcdef ghijk'
              >>> sList = []
              >>> for letter in s:
              ... 	if letter == " ":
              ... 		sList.append(".")
              ... 	else:
              ... 		sList.append(letter)
              ... 		
              >>> "".join(sList)
              'abcdef.ghijk'
              >>> s
              'abcdef ghijk'
              >>> s1 = ''
              >>> for letter in s:
              ... 	if letter == " ":
              ... 		s1 += "."
              ... 	else:
              ... 		s1 += letter
              ... 		
              >>> s1
              'abcdef.ghijk'
              >>>

              Comment

              Working...