In Python, how can I remove characters around a specific character?

python remove character from string by index
remove multiple characters from string python
remove character from list python
remove special characters from list python
remove special characters from string python
python remove first character from string
remove special characters from dataframe python
python remove last character from string

I have a list that looks like the following. Within each item of the list, separate items are delimited by a semicolon, but there the amount of space surrounding each semicolon seems to be random:

['New Jersey  ;   46.3% ;  Republican ;         03/10/2015', 'Pennsylvania ; 
      39.0%; Democrat    ;04/30/2012', 'Virginia .   ;54.7% ;Independent 
   ;10/25/10',       'Maryland;44.8%   ;        Democrat; 01/15/16', 'New York;  R50.9%; Republican ;                  09/22/15'] 

I would like the final output to be a list that looks like the following:

['New Jersey;46.3%;Republican;03/10/2015', 'Pennsylvania;39.0%;Democrat;04/30/2012', 'Virginia;54.7%;Independent;10/25/10' ... ]

I've been trying .split(), but that's not dropping the characters in the middle. Is doing a .replace() on each possible combination of spaces and semicolons my only hope?


Here's a short way of doing it. This one line should be enough.

s = ['New Jersey  ;   46.3% ;  Republican ;         03/10/2015', 'Pennsylvania ; 39.0%; Democrat    ;04/30/2012', 'Virginia .   ;54.7% ;Independent ;10/25/10',       'Maryland;44.8%   ;        Democrat; 01/15/16', 'New York;  R50.9%; Republican ;                  09/22/15']

new_list = [';'.join([word.strip() for word in item.split(';')]) for item in s]

And here's the Expanded Form.

new_list = []

for item in s:
    sub_list = [word.strip() for word in item.split(';')]
    new_list.append(';'.join(sub_list))

print(new_list)

Outputs:

['New Jersey;46.3%;Republican;03/10/2015', 'Pennsylvania;39.0%;Democrat;04/30/2012', 'Virginia .;54.7%;Independent;10/25/10', 'Maryland;44.8%;Democrat;01/15/16', 'New York;R50.9%;Republican;09/22/15']

Python Remove Character from String, Python Remove Character from String using replace()​​ We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to remove all characters after a specific character in python?


Use the replace function:

>>> new_list = [val.replace(' ', '') for val in old_list]

Edit: As pointed out, this removes spaces in words like "New Jersey". Instead, use a regex replace:

>>> import re
>>> new_list = [re.sub(' +\.', '', re.sub(' *; *', ';', val)) for val in old_list]
>>> new_list
 ['New Jersey;46.3%;Republican;03/10/2015',
 'Pennsylvania;39.0%;Democrat;04/30/2012',
 'Virginia;54.7%;Independent;10/25/10',
 'Maryland;44.8%;Democrat;01/15/16',
 'New York;R50.9%;Republican;09/22/15']

Python, The generic problem faced by the programmers is removing a character from the entire These can be in the form of special characters for reconstructing valid  Python - How to delete rest of string after a specific word/character 0 To remove strings in between quotes and inserting a new line before a special character from a text file and saving it


old_list = ['New Jersey  ;   46.3% ;  Republican ;         03/10/2015', 'Pennsylvania ; 
  39.0%; Democrat    ;04/30/2012', 'Virginia .   ;54.7% ;Independent 
  ;10/25/10',       'Maryland;44.8%   ;        Democrat; 01/15/16', 'New York;    R50.9%; Republican ;                  09/22/15'] 


for row in old_list:
     data = [words.strip() for words in row.split(";")]
     old_list[old_list.index(row)] = ";".join(data)

Ways to remove i'th character from string in Python, replace() can possibly be used for performing the task of removal as we can replace the particular index with empty char, and hence solve the issue. Drawback:  The string class has a method replace that can be used to replace substrings in a string. We can use this method to replace characters we want to remove with an empty string. For example: >>> "Hello people".replace("e", "") "Hllo popl" If you want to remove multiple characters from a string in a single line, it's better to use regular expressions.


Combined use of re.sub() and replace():

re.sub(r"\s*([;,])\s*",r"\1",txt).replace(",",", ")

Removing characters before, after, and in the middle of strings, I find these three methods can solve a lot of your problems: Let's walk… When working with real-world datasets in Python and pandas, you will need to remove characters from your .split() #splits the string into two tuples around whatever character it was given and deletes that character..lstrip() #strips  How to remove characters from a string, remove substring, remove special characters, remove newline character, remove space and remove multiple characters in python? String manipulation is very essential for a python developer. In this article, we have explained important string manipulation with a set of examples.


How to remove specific characters from a string in Python?, The string class has a method replace that can be used to replace substrings in a string. We can use this method to replace characters we want  Python Remove Character from String. Using string replace() function; Using string translate() function; Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.


Delete substrings within strings - MATLAB erase, Starting in R2017a, you can create strings using double quotes. Text to delete, specified as a string array, a character vector, or a cell array of character vectors. Partition large arrays across the combined memory of your cluster using  To Remove Character From String In Python, we can use string replace() or string translate() method. In Python, the string object is immutable and hence sometimes poses visible restrictions while coding the constructs that are required in day-day programming.


Python 3 Notes: Split and Join, Whitespace characters include space ' ', the newline character '\n', and tab '\t', parameter, .split('x') can be used to split a string on a specific substring 'x'. In this article we will discuss how to remove characters from a string at specific index position or in a given range of indices. We can remove characters from string by slicing the string into pieces and then joining back those pieces. String Slicing. In Python strings are immutable i.e. we can not modify the string objects.