Selecting first word of a variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Boblert
    New Member
    • Aug 2007
    • 4

    Selecting first word of a variable

    How do I select the first word of a variable?[CODE=python]
    name = raw_input("What 's your full name? ")
    print "Hey, %s, how you doing?" % name[/CODE]

    So say I only wanted it to print the first name? How would I do this not knowing the amount of letters in it?
    Last edited by bartonc; Aug 19 '07, 09:23 PM. Reason: Added [CODE=python][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by Boblert
    How do I select the first word of a variable?[CODE=python]
    name = raw_input("What 's your full name? ")
    print "Hey, %s, how you doing?" % name[/CODE]

    So say I only wanted it to print the first name? How would I do this not knowing the amount of letters in it?
    [CODE=python]
    >>> name = "Jo Shcmo"
    >>> firstandlast = name.split()
    >>> firstname = firstandlast[0]
    >>> firstname
    'Jo'
    >>> [/CODE]

    Comment

    • Boblert
      New Member
      • Aug 2007
      • 4

      #3
      Thanks alot,
      also how would I create a function to replace but only if its the whole words for example:

      Replace:
      man : guy

      Sentence to be replaced:
      That man is good but he ain't no superman.

      But I can only get it to replace as:
      That guy is good but he ain't no superguy.

      But I want it to replace as:
      The guy is good but he ain't no superman.


      Sorry if that my problem is confusing its hard to explain

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by Boblert
        Thanks alot,
        also how would I create a function to replace but only if its the whole words for example:

        Replace:
        man : guy

        Sentence to be replaced:
        That man is good but he ain't no superman.

        But I can only get it to replace as:
        That guy is good but he ain't no superguy.

        But I want it to replace as:
        The guy is good but he ain't no superman.


        Sorry if that my problem is confusing its hard to explain
        Try something like this:
        [code=python]
        sent = "That man is good but he ain't no superman."
        words = sent.split()

        for (i, word) in enumerate(words ):
        if word == "man":
        words[i] = "guy"

        newsent = " ".join(word s)
        [/code]
        You could also use the re module.

        Comment

        • Boblert
          New Member
          • Aug 2007
          • 4

          #5
          Ye, thats what I've been doing. But it is a large word-replace list and large paragraph.
          Just thought there might of been a way with less lines used :D
          and I don't quite understand the re module

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by Boblert
            Ye, thats what I've been doing. But it is a large word-replace list and large paragraph.
            Just thought there might of been a way with less lines used :D
            and I don't quite understand the re module
            If you have more than one word you can do something like this (don't know if you did it already) ;):
            [code=python]
            replacedict = {"man": "guy", "woman": "girl", "car": "automobile "} # etc...

            for i in range(len(parag raph)):
            if paragraph[i] in replacedict.key s():
            paragraph[i] = replacedict[paragraph[i]]
            [/code]
            Technically, that uses the same amount of lines.

            Comment

            • Boblert
              New Member
              • Aug 2007
              • 4

              #7
              Thanks alot thats solved my problem :D

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by Boblert
                Ye, thats what I've been doing. But it is a large word-replace list and large paragraph.
                Just thought there might of been a way with less lines used :D
                and I don't quite understand the re module
                A Regular Expression (re), has it's own language. A VERY simple example would be a sub-string followed by any whitespace (pure Python could easily do this to, but "regex"s, as they are called go much further):[CODE=python]>>> import re
                >>> s = "That man is good but he ain't no superman"
                >>> reObj = re.compile("man \s")
                >>> reObj.sub("guy ", s)
                "That guy is good but he ain't no superman"
                >>> [/CODE]Just thought I'd throw that out there.

                Comment

                • ghostdog74
                  Recognized Expert Contributor
                  • Apr 2006
                  • 511

                  #9
                  Originally posted by Boblert
                  Thanks alot,
                  also how would I create a function to replace but only if its the whole words for example:

                  Replace:
                  man : guy

                  Sentence to be replaced:
                  That man is good but he ain't no superman.

                  But I can only get it to replace as:
                  That guy is good but he ain't no superguy.

                  But I want it to replace as:
                  The guy is good but he ain't no superman.


                  Sorry if that my problem is confusing its hard to explain
                  read the docs for replace() more carefully. The docs are your good friend.
                  Code:
                  >>> "This man is a superman".replace("man","guy",1)
                  'This guy is a superman'
                  >>> "This man is a superman".replace("man","guy")
                  'This guy is a superguy'
                  >>>

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by ghostdog74
                    read the docs for replace() more carefully. The docs are your good friend.
                    Code:
                    >>> "This man is a superman".replace("man","guy",1)
                    'This guy is a superman'
                    >>> "This man is a superman".replace("man","guy")
                    'This guy is a superguy'
                    >>>
                    "superguy", that's a good one!

                    Comment

                    Working...