This question already has answers here:
Remove quotes from String in Python
(8 answers)
Closed 3 years ago.
So, I have a text string that I want to remove the "" from.
Here is my text string:
string= 'Sample this is a string text with "ut" '
Here is the output I want once using a regex expression:
string= 'Sample this is a string text with ut'
Here is my overall code:
import re
string= 'Sample this is a string text with "ut" '
re.sub('" "', '', string)
And the output just show the exact text in the string without any changes. Any suggestions?
You can simply use string.replace('"','')
If you want just remove all " symbols, you can use str.replace instead:
string = string.replace('"', '')
Related
This question already has answers here:
Python strip() multiple characters?
(7 answers)
Closed 11 months ago.
here is my code:
string = input("Enter a string:")
print(string)
characters = input("Enter the characters you would like to make disappear:")
print(characters)
print(string.replace("characters"," "))
I need to display variable string without the entered characters in variable characters
You need to iterate over the characters, and replace each one by one
Example
for ch in "characters":
string.replace(ch, "")
Example using re to remove all vowels in your string:
>>> import re
>>> characters_to_remove = "aeiouy"
>>> my_string = "my string"
>>> re.sub(f"[{characters_to_remove}]", "", my_string)
'm strng'
This question already has answers here:
How do I escape backslash and single quote or double quote in Python? [duplicate]
(5 answers)
Closed 2 years ago.
I'm trying to print out a list and strip the " ' " (single quotes) character from a list I generated from a CSV File.
Here's the code I have
import numpy as np
import pandas as pd
export = pd.read_csv('File') #open file
skuList = export.values.T[0].tolist() #transpose DF + convert to list
#print(skuList)
skuList = [value.strip(" ' ") for value in skuList] #strip ' ' '
print(skuList)
The output from this program is
'GL2i-RS-36', '523-30', 'RK623-30', ....
The output that I would want would be:
GL2i-RS-36, 523-30, RK623-30, ....
Is there a way to print out a list without the single quotes?
Maybe try this (without the spaces around the '):
skuList = [value.strip("'") for value in skuList] #strip ' ' '
This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 7 years ago.
Ponder that you have a string which looks like the following 'This string is {{}}' and you would like to transform it into the following 'This string is {wonderful}'
if you do 'This string is {{}}'.format('wonderful') it won't work. What's the best way to achieve this?
You just need one more pair of {}
'This string is {{{}}}'.format('wonderful')
you need triple brackets: two for the literal { and }and the pair in the middle for the format function.
print('This string is {{{}}}'.format('wonderful'))
Two brackets to get {} in line (escaping), and third as placeholder:
'This string is {{{}}}'.format('wonderful')
You can do this: print "{{f}}".format(f='wonderful').
You can do this as well: "Hello, {name}!".format(name='John'). This will substitute all {name}s with John.
This question already has answers here:
Split a string by spaces -- preserving quoted substrings -- in Python
(16 answers)
Closed 9 years ago.
I have my data as below
string = ' streptococcus 7120 "File being analysed" rd873 '
I tried to split the line using n=string.split() which gives the below result:
[streptococcus,7120,File,being,analysed,rd873]
I would like to split the string ignoring white spaces in " "
# output expected :
[streptococcus,7120,File being analysed,rd873]
Use re.findall with a suitable regex. I'm not sure what your error cases look like (what if there are an odd number of quotes?), but:
filter(None, it.chain(*re.findall(r'"([^"]*?)"|(\S+)', ' streptococcus 7120 "File being analysed" rd873 "hello!" hi')))
> ['streptococcus',
'7120',
'File being analysed',
'rd873',
'hello!',
'hi']
looks right.
You want shlex.split, which gives you the behavior you want with the quotes.
import shlex
string = ' streptococcus 7120 "File being analysed" rd873 '
items = shlex.split(string)
This won't strip extra spaces embedded in the strings, but you can do that with a list comprehension:
items = [" ".join(x.split()) for x in shlex.split(string)]
Look, ma, no regex!
This question already has answers here:
Is there a simple way to remove multiple spaces in a string?
(27 answers)
Closed 6 years ago.
I wanna know how to remove unwanted space in between a string. For example:
>>> a = "Hello world"
and i want to print it removing the extra middle spaces.
Hello world
This will work:
" ".join(a.split())
Without any arguments, a.split() will automatically split on whitespace and discard duplicates, the " ".join() joins the resulting list into one string.
Regular expressions also work
>>> import re
>>> re.sub(r'\s+', ' ', 'Hello World')
'Hello World'