473,395 Members | 1,466 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,395 software developers and data experts.

removing numbers from string

Thekid
145 100+
Hi.....if I have a long string of numbers, letters, symbols, how can I remove the numbers but put them aside for use later on in the code? Example of string:

e#pf3$@hzfvlkfsyx?1pdidasifkckyil@#6s0sod#9$uqwnb5 u95f@0c9m099@tt?3

-Thanks
Feb 4 '09 #1
5 10607
bvdet
2,851 Expert Mod 2GB
If you have multiple strings to extract the digits, you can save them in a list. Otherwise, save the result by creating a single object reference. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'e#pf3$@hzfvlkfsyx?1pdidasifkckyil@#6s0sod#9$uqwnb5 u95f@0c9m099@tt?3'
  2. >>> sdigits = ''.join([letter for letter in s if letter.isdigit()])
  3. >>> sdigits
  4. '31609595090993'
  5. >>> 
Feb 4 '09 #2
Thekid
145 100+
That works just fine :) Thank you I was trying some ''.join but couldn't get it right.
Feb 4 '09 #3
Thekid
145 100+
I have one more question on this.....after I 'print sdigits' and get the numbers, I try to 'print s' and the numbers are still there. How can I get it to where the numbers are removed from the string so I have 2 separate lists, one containing the numbers and one containing whats left? I've tried things like:

s.replace
s.split
s.lstrip

I can get it to take away one number with s.replace("1","") but that's it. If I try to make it:
s.replace("('1','2','3','4','5','6','7','8','9')", "")
it doesn't work.
Feb 4 '09 #4
bvdet
2,851 Expert Mod 2GB
Do it the same way, but add Python keyword not.
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'e#pf3$@hzfvlkfsyx?1pdidasifkckyil@#6s0sod#9$uqwnb5 u95f@0c9m099@tt?3'
  2. >>> sdigits = ''.join([letter for letter in s if letter.isdigit()])
  3. >>> severythingelse = ''.join([letter for letter in s if not letter.isdigit()])
  4. >>> severythingelse
  5. 'e#pf$@hzfvlkfsyx?pdidasifkckyil@#ssod#$uqwnb uf@cm@tt?'
  6. >>> 
Feb 4 '09 #5
Thekid
145 100+
Oh ok! Thanks again.
Feb 5 '09 #6

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

Similar topics

2
by: D. Alvarado | last post by:
Hello, I have an array that contains numbers. Each element in the array is guaranteed to be unique. Let's say I have another variable which I know for certain is in the array, but I don't know...
2
by: Brian Henry | last post by:
Is there any quick and easy (other then scanning a string char by char) to remove all the alpha chars from a string and leave only numbers? ex: A12334234-3431AP comes out to 123342343431...
23
by: Peter Row | last post by:
Hi, I am currently working on a VB.NET project that has been going for quite a while. In the past it has been developed with a lot of compatibility VB6 functions and constants, e.g Left(),...
4
by: Yosh | last post by:
I have a string where I want to remove only those characters that are letters or numbers. Does .NET provide an easy method to do this? TIA!
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
1
by: Kosmos | last post by:
Since people have been so helpful on this site I thought I'd contribute what little I can...since I am not a programmer but I'm working on an access database for work Anyways I had to remove...
4
by: Ahmed, Shakir | last post by:
I need to remove text string from the list of the numbers mentioned below: 080829-7_A 070529-5_c 080824-7_O 070405_6_p The output will be : 080829-7 070529-5
0
by: Adam Pletcher | last post by:
You just want to drop the last two characters? Slice it. 080829-7 - Adam Behalf
2
by: wwtan | last post by:
I want to remove the numbers and letters on the left on a string. I have some strings which read as such: ATT 2393747394 AT&T 2732372Tom CAN Air38263 AA3333333
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.