473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

str information

20
How to print only the number in:

x = 1p
if I print x = 1p
but in want only = 1

Thank You
Sep 17 '08 #1
5 1260
boxfish
469 Expert 256MB
I'm guessing you mean x = "1p"?
You're trying to take an integer value off the front of a string, right?
So if x = "123abc" you want 123, and if x = "10307a10c52" you want 10307, right? I don't know of a function that does this for you in Python, but I can think of some code that would, although it probably isn't very efficient code. You could make a loop that takes larger portions of the string each iteration, tries to convert them to integers with the int() function, and ends when it catches a ValueError. Here it is, for what it's worth:
Expand|Select|Wrap|Line Numbers
  1. x = "123abc"
  2. x_number = 0
  3. for i in xrange(1, len(x)):
  4.     try:
  5.         x_number = int(x[:i])
  6.     except ValueError:
  7.         break
  8. print x_number
  9.  
I also googled for "Python, atoi", and found this archived thread, in which reply #3 has some code that does this too.

Hope this helps.
Sep 17 '08 #2
bvdet
2,851 Expert Mod 2GB
Below are three ways of getting the numbers from a string. The first one gets all the numbers, and the other two get only the leading numbers.
Expand|Select|Wrap|Line Numbers
  1. x = "123abc456"
  2. print ''.join([c for c in x if c.isdigit()])
  3.  
  4. import re
  5. patt = re.compile(r'^([0-9]+)')
  6. print patt.search(x).group(1)
  7.  
  8. x_num = ''
  9. for c in x:
  10.     if c.isdigit():
  11.         x_num += c
  12.     else:
  13.         break
  14. print x_num
Output:
>>> 123456
123
123
Sep 17 '08 #3
nicstel
20
It was exactly what I wanted

Thank you at both of you
Sep 17 '08 #4
ghostdog74
511 Expert 256MB
KISS
Expand|Select|Wrap|Line Numbers
  1. >>> import string
  2. >>> x="abc123"
  3. >>> x.strip(string.letters)
  4. '123'
  5. >>> x="123abc"
  6. >>> x.strip(string.letters)
  7. '123'
  8. >>>
  9.  
Sep 20 '08 #5
nicstel
20
Thank ghostdog74,

It's more simple. Have a nice day.
Sep 22 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Emine Ekin | last post by:
/*Apologize for multiple posts*/ FIRST CALL FOR PAPERS ADVIS 2006 Fourth Biennial International Conference on Advances in Information Systems 18-20 October, 2006 Izmir, Turkey
8
by: Raquel | last post by:
The "DB2 Information Center" (http://publib.boulder.ibm.com/infocenter/db2help/index.jsp) seems to be an amazing resource and as far as I could tell (after browsing through various sections of...
5
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for...
6
by: Markus Wildgruber | last post by:
Hi! How can I get information about the system my application is running on? I found the System.Environment class that provides me with some of the desired information but some of them not in...
6
by: Ian Williamson | last post by:
Greetings, My company has an ASP.NET based enterprise product that is undergoing some changes and I need some community input to help solve a problem. In the current implementation, any given...
0
by: Derek | last post by:
I am creating an intranet using Visual Web Developer Express Edition. Everything has been working OK until yesterday when I started getting 62 messages all beginning "Could not find schema...
1
by: keke3905 | last post by:
I really would appreciate some help on this assignment. I need to make GUI boxes to display the system info such as on Microsoft Office. I have some code but not sure where to go with the rest of...
0
by: Lester Knutsen | last post by:
A two-day IBM Informix and DB2 User Group Technical Conference - Friday and Saturday, December 8-9, 2006 Location - Fairview Park Marriott, Falls Church, VA (near Washington D.C.)...
10
by: =?Utf-8?B?SmFtZXMgV29uZw==?= | last post by:
Hi everybody, I'm trying to use the new VB 2008 right now and I want to know how to preset the company name and copyright informtion in Assembly Information. In my current VB 2005, company name...
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.