Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 20th, 2012, 05:18 AM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 111 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 4 m 4 sec
Reputation Power: 1
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!

Reply With Quote
  #2  
Old November 20th, 2012, 08:30 AM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 37 m 44 sec
Reputation Power: 2
Quote:
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

Reply With Quote
  #3  
Old November 20th, 2012, 08:43 AM
Quackajack Quackajack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 35 Quackajack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 55 m 6 sec
Reputation Power: 1
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).

Reply With Quote
  #4  
Old November 20th, 2012, 02:13 PM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 111 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 4 m 4 sec
Reputation Power: 1
Thanks guys.
that was very helpful!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > 2 newbie questions; input and string manipulations

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap