#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    New python user help!


    Hey guys, i recently started taking python programming classes and I have 2 questions that I am having difficulty solving. The two problems are :

    1. Without using the method center, get a string from the user and a field size then display the string centered within that field, with dots "." on either side. You may assume that the field size is larger than the length of the string.

    2. Get the user's full name with one raw_input and display the name as last, first

    These two questions are about string manipulation.

    Any help would be appreciated =D
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    618
    Rep Power
    65
    Code:
    name = "Oscar Meyer"
    print("{1}, {0}".format(*name.split()))  # Meyer, Oscar
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,963
    Rep Power
    481

    Selling "test first" methodology.


    Code:
    # use with doctest
    
    # $ python -m doctest -v thisFile.py
    
    def center(text,width,fill='.'):
        '''
            Three leading dots screws with doctest.
            Changing the fill is easier than reading the manual.
    
            >>> print(center('hi',8,fill='-'))
            ---hi---
    
            >>> print(center('the',4))
            the.
        '''
        assert len(text) <= width # verify problem condition
        n = (width - len(text)) // 2
        full = n * fill
        return (full + text + full + fill)[:width]
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo