This question already has answers here:
Split a string by spaces -- preserving quoted substrings -- in Python
(16 answers)
Closed 12 months ago.
Is it possible to split() string at " "? I have tried
x=str(input("Enter string to split: ")) #Input: one "two three"
xsplit=str.split()
print(xsplit[1])
but it returns "two" but I want it to show "two three".
How to do it?
This will let you split on the space as well as gather everything after the split.
x=str(input("Enter string to split: ")) #Input: one "two three"
xsplit = x.split(' ')[1:]
xjoin = ' '.join(xsplit)
print(xjoin)
Once you have split into a list you can rejoin the list into a single string with .join(xsplit)
Related
This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 3 years ago.
So, I have a string "AB256+74POL". I want to extract the numbers only into a list say num = [256,74]. How to do this in python?
I have tried string.split('+') and followed by iterating over the two parts and adding the characters which satisfy isdigit(). But is there an easier way to that?
import re
a = 'AB256+74POL'
array = re.findall(r'[0-9]+', a)
"".join([c if c.isdigit() else " " for c in mystring]).split()
Explanation
Strings are iterable in python. So we iterate on each character in the string, and replace non digits with spaces, then split the result to get all sequences of digits in a list.
This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 4 years ago.
I want to when I input a string, if words in string is contain in a list of words, delete words in the strings
Or, you could do the following:
raw_string = input("Enter String:")
useless_list = ["Birds", "Cat"]
print(' '.join([i for i in raw_string.split() if i not in useless_list]))
This question already has answers here:
How to find and replace nth occurrence of word in a sentence using python regular expression?
(9 answers)
Closed 6 years ago.
I'm looking to remove a ',' (comma) from a string, but only the second time the comma occurs as it needs to be in the correct format for reverse geocoding...
As an example I have the following string in python:
43,14,3085
How would I convert it to the following format:
43,143085
I have tried using regex and str.split() but have not achieved result yet..
If you're sure that string only contains two commas and you want to remove the last one you can use rsplit with join:
>>> s = '43,14,3085'
>>> ''.join(s.rsplit(',', 1))
'43,143085'
In above rsplit splits starting from the end number of times given as a second parameter:
>>> parts = s.rsplit(',', 1)
>>> parts
['43,14', '3085']
Then join is used to combine the parts together:
>>> ''.join(parts)
'43,143085'
What about something like:
i = s.find(',')
s[:i] + ',' + s[i+1:].replace(",", "")
This will get rid of all your commas excepts the first one:
string = '43,14,3085'
splited = string.split(',')
string=",".join(splited[0:2])
string+="".join(splited[2:])
print(string)
This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Closed 6 years ago.
I want to transform the string 'one two three' into one_two_three.
I've tried "_".join('one two three'), but that gives me o_n_e_ _t_w_o_ _t_h_r_e_e_...
how do I insert the "_" only at spaces between words in a string?
You can use string's replace method:
'one two three'.replace(' ', '_')
# 'one_two_three'
str.join method takes an iterable as an argument and concatenate the strings in the iterable, string by itself is an iterable so you will separate each character by the _ you specified, if you directly call _.join(some string).
You can also split/join:
'_'.join('one two three'.split())
And if you want to use join only , so you can do like thistest="test string".split()
"_".join(test)
This will give you output as "test_string".
This question already has answers here:
Splitting string and removing whitespace Python
(2 answers)
Closed 9 years ago.
tgtPorts = str(options.tgtPort).split(', ')
I'm trying to split a string tgtPort that could look like 21, 80, 139
According to the website I was looking at, the above should split that string into a list containing each individual element IE: 139
However using:
for tgtPort in tgtPorts:
print tgtPort + "\n"
I find that my list only contains 21,
How can I ensure that the comma and the space are removed?
How can I ensure that all elements will end up in my list and not just the first one?
Spelling out Robert's advice:
tgtPorts = [s.strip() for s in str(options.tgtPort).split(',')]