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

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    2 newbie questions; input and string manipulations


    Hi guys,
    im new to python and i can really use some help with it.

    my first question:
    is there a simple way to obtain as input three integers from user with one command?
    im talking about something equivalent to the c command where you can obtain as many inputs as you want with just one line:
    Code:
    scanf("%d %d %d %d...", &a, &b, &c, &d...)
    my second question:
    where can i find a list of functions that can manipulate strings?
    for example, lets say im getting from the user an integer number (with raw_input) - and i want to access one of the memory cells (i assume python saves each digit on 4 bits memory cell) and change its value as i please.
    or - another example - i want to count how many digits are there.
    or - i want to evaluate if the string doesn't hold any signs(char), in other words - to make sure it holds only digits.

    are these operations possible?

    thanks in advanced, and have a nice day!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Originally Posted by so.very.tired
    Hi guys,
    im new to python and i can really use some help with it.

    my first question:
    is there a simple way to obtain as input three integers from user with one command?
    im talking about something equivalent to the c command where you can obtain as many inputs as you want with just one line:
    Code:
    scanf("%d %d %d %d...", &a, &b, &c, &d...)
    my second question:
    where can i find a list of functions that can manipulate strings?
    for example, lets say im getting from the user an integer number (with raw_input) - and i want to access one of the memory cells (i assume python saves each digit on 4 bits memory cell) and change its value as i please.
    or - another example - i want to count how many digits are there.
    or - i want to evaluate if the string doesn't hold any signs(char), in other words - to make sure it holds only digits.

    are these operations possible?

    thanks in advanced, and have a nice day!
    First, it's not very pretty but:
    Code:
    >>> a,b,c=map(int,raw_input("enter three numbers separated by commas: ").split(','))
    enter three numbers separated by commas: 12,24,42
    >>> a
    12
    >>> b
    24
    >>> c
    42
    >>>

    Second,
    http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange

    see:5.6.1 String Methods
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    39
    Rep Power
    3
    I'm not aware of a direct replacement for scanf in python but if you know what the separate is going to be, you can split the input by the separator and the eval it.

    As an example to get a list of all numbers entered seperated by a space:
    Code:
    numlist = [eval(x) for x in raw_input().split(' ')]
    If you are using Python 3.x use input() instead of raw_input. You can use int() or float() etc instead of eval to get all numbers of the same type. This will throw an exception if invalid input is entered.

    The online python documentation contains all the standard methods for string manipulation. Don't worry about how the string is internally represented, just use the methods given.

    Remember that a string is immutable (read-only) so you can access each character with [] but not change it. You can also loop through the string with for.

    As an example this will count the number of digits in an easy to understand way:
    Code:
    count = 0
    digits = '0123456789'
    for char in raw_input():
        if char in digits:
            count+=1
    A more experienced Pythoner would use filter and lamda. Or you could use a regular expression (see the re module).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Thanks guys.
    that was very helpful!

IMN logo majestic logo threadwatch logo seochat tools logo