Removing numbers from string
Would this work for your situation?
>>> s = '12abcd405'
>>> result = ''.join([i for i in s if not i.isdigit()])
>>> result
'abcd'
This makes use of a list comprehension, and what is happening here is similar to this structure:
no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
if not i.isdigit():
no_digits.append(i)
# Now join all elements of the list with '',
# which puts all of the characters together.
result = ''.join(no_digits)
As @AshwiniChaudhary and @KirkStrauser point out, you actually do not need to use the brackets in the one-liner, making the piece inside the parentheses a generator expression (more efficient than a list comprehension). Even if this doesn't fit the requirements for your assignment, it is something you should read about eventually :) :
>>> s = '12abcd405'
>>> result = ''.join(i for i in s if not i.isdigit())
>>> result
'abcd'
Removing all numeric characters in a string, python
You can find the answer here
Removing numbers from string
From this answer:
comp_string = "xxf1,aff242342"
new_string = ''.join([i for i in comp_string if not i.isdigit()])
It creates a new string using .join from a list. That list is created using a list comprehension that iterates through characters in your original string, and excludes all digits.
How to remove numbers from string terms in a pandas dataframe
You can apply str.replace to the Name
column in combination with regular expressions:
import pandas as pd
# Example DataFrame
df = pd.DataFrame.from_dict({'Name' : ['May21', 'James', 'Adi22', 'Hello', 'Girl90'],
'Volume': [23, 12, 11, 34, 56],
'Value' : [21321, 12311, 4435, 32454, 654654]})
df['Name'] = df['Name'].str.replace('\d+', '')
print(df)
Output:
Name Value Volume
0 May 21321 23
1 James 12311 12
2 Adi 4435 11
3 Hello 32454 34
4 Girl 654654 56
In the regular expression \d
stands for "any digit" and +
stands for "one or more".
Thus, str.replace('\d+', '')
means: "Replace all occurring digits in the strings with nothing".
Remove numbers from string in dataframe in R
You can do this with gsub
in base R :
df$ColA <- gsub('[0-9.]', '', df$ColA)
df
# ColA ColB
#1 A dff
#2 B dfa
#3 C dfd
data
df <- structure(list(ColA = c("A.12", "B.34", "C.545"), ColB = c("dff",
"dfa", "dfd")), class = "data.frame", row.names = c(NA, -3L))
Removing numbers from string while keeping alphanumeric words
You can use regex to find runs of one or more digits \d+
between two word boundaries \b
, and replace them with nothing.
>>> import re
>>> string = "Jobid JD123 has been abended with code 346"
>>> re.sub(r"\b\d+\b", "", string).strip()
'Jobid JD123 has been abended with code'
Note that the regex doesn't get rid of the trailing space (between "code"
and the digits), so you need to strip()
the result of the re.sub()
.
How to remove numbers from a string?
Very close, try:
questionText = questionText.replace(/[0-9]/g, '');
replace
doesn't work on the existing string, it returns a new one. If you want to use it, you need to keep it!
Similarly, you can use a new variable:
var withNoDigits = questionText.replace(/[0-9]/g, '');
One last trick to remove whole blocks of digits at once, but that one may go too far:
questionText = questionText.replace(/\d+/g, '');
Strip Numbers From String in Python
Yes, you can use a regular expression for this:
import re
output = re.sub(r'\d+', '', '123hello 456world')
print output # 'hello world'
Related Topics
Nested for Loops Using List Comprehension
Opencv Real Time Streaming Video Capture Is Slow. How to Drop Frames or Get Synced with Real Time
Easy Way of Finding Decimal Places
How to Set a Default Parameter Equal to Another Parameter Value
Full Examples of Using Pyserial Package
Str' Object Has No Attribute 'Decode'. Python 3 Error
Escape Special Characters in a Python String
Store Mouse Click Event Coordinates with Matplotlib
Sum of List of Lists; Returns Sum List
Cannot Install Python 3.7 on Osx-Arm64
Group by & Count Function in SQLalchemy
How to Request a Url in Python and Not Follow Redirects
Asyncio.Sleep() VS Time.Sleep()
How to Change Plot Background Color
How to Enumerate an Object's Properties in Python