I have the string name = 'one two'. i want to make 'onetwo' from it.
is there any cool python shortcut for it like .join() but without space?
You can do name.replace(' ','') or ''.join(name.split())
You could do this by just striping all whitespace in a string:
name.replace(" ", "")
How about "".join(name.split(" ")) ?
Another one, only using one empty string and no explicit whitespace:
"".join('one two'.split())
Result:
'onetwo'
Related
I try to figure out how I can delete certain characters from a string. Unfortunately, it doesn't work. I would appreciate all the help.
def delete_char(string):
string = list(string)
string.remove("\n")
return ''.join(string)
delete_char("I want \n to test \n if you \n work")
How about using replace, instead?
def delete_char(string, target_char, replacement_char=""):
return string.replace(target_char, replacement_char)
print(delete_char("I want \n to test \n if you \n work", "\n"))
You need to re-assign the string value to the removed form. Additionally I would suggest using replace instead of remove in this place, and replacing it with an empty character. Something like this should work:
def delete_char(string):
string = string.replace("\n", "")
return string
You could use str.split and str.join:
>>> ' '.join("I want \n to test \n if you \n work".split())
I want to test if you work
This isn't the same as just removing the newline character but it will ensure only one space between words.
Otherwise just replace the newline with nothing:
>>> "I want \n to test \n if you \n work".replace('\n', '')
I want to test if you work
input string
str = "(\"Cardinal\", \"Tom B. Erichsen\", \"Skagen 21\",)"
output string should look like:
("Cardinal", "Tom B. Erichsen", "Skagen 21")
The comma at the end should be removed, help me how to do this in python code.
I tried with str.rstrip(",") it dint work.
You can use some regex for example you can replace (.*),([^,]+)$ with \1\2
result = re.sub(r"(.*),([^,]+)$", r"\1\2", yourstring)
here is a regex demo
Check this code
str = str.replace('",)', '")')
you can chain different str.replace()
str.replace(", )",")").replace(",)",")")
That will work for your string
You can do this in following way
str = "(\"Cardinal\", \"Tom B. Erichsen\", \"Skagen 21\",)"
str = str[:len(str)-2] + str[len(str)-1]
You could use the regex module:
import re
s = "INSERT INTO Customers (CustomerName, ContactName, Address, ) VALUES (\"Cardinal\", \"Tom B. Erichsen\", \"Skagen 21\",)"
print re.sub(r',(\s+)*\)', ')', s)
I have a string, which after a character I wish to remove everything after the character. However, the issue is that I have multiple characters like this in the string and its only the characters after the last one which I wish to remove.
for example:
str = "howdie how are you? are you good? sdfsdf"
str = str.RemoveEverythingAfterLast("?")
str = "howdie how are you? are you good?"
I was wondering if there was an efficient way to do this in python? I had thought of looping backwards through the string deleting characters 1 by 1 until I found the character I was looking for (in example the '?'). But I was wondering if there was a more efficient way to go about this?
Use str.rpartition():
''.join(string.rpartition('?')[:2])
Demo:
>>> string = "howdie how are you? are you good? sdfsdf"
>>> ''.join(string.rpartition('?')[:2])
'howdie how are you? are you good?'
Using regex:
str = re.sub("(.*\?).*", "\\1", str)
capturing the group till the last ? and replace it with captured group \\1.
You can use str.rfind and slicing:
>>> string = "howdie how are you? are you good? sdfsdf"
>>> string[:string.rfind("?") + 1]
'howdie how are you? are you good?'
>>>
The + 1 will cause the ? to be left on the end of the returned string.
Input
str = 'test1,test2,test3,'
Ouput
str = 'test1,test2,test3'
Requirement to strip the last occurence of ','
Just use rstrip().
result = your_string.rstrip(',')
str = 'test1,test2,test3,'
str[:-1] # 'test1,test2,test3'
The question is very old but tries to give the better answer
str = 'test1,test2,test3,'
It will check the last character, if the last character is a comma it will remove otherwise will return the original string.
result = str[:-1] if str[-1]==',' else str
Though it is little bit over work for something like that. I think this statement will help you.
str = 'test1,test2,test3,'
result = ','.join([s for s in str.split(',') if s]) # 'test1,test2,test3'
If you have to remove the last comma (also as the last character?) you can do this via the function removesuffix()
Here is an example:
>>>'hello,'.removesuffix(',')
'hello'
Actually we have to consider the worst case also.
The worst case is,
str= 'test1,test2,test3, ,,,, '
for above code, please use following code,
result = ','.join([s.strip() for s in str.split(',') if s.strip()!=''])
It will work/remove the prefix 'comma' also. For example,
str= ' , ,test1,test2,test3, ,,,, '
I wish to split a long list of email address into lines, such as:
test01#testing.com; test02#testing.com; test03#testing1.com; test04#testing2.com,
I wish to split them into lines:
test01#testing.com (carriage return)
test02#testing.com (carriage return)
test03#testing1.com (carriage return)
test04#testing2.com
Can anyone pls help? Thanks.
You can use split to split by semi-colon and strip to remove any spaces:
>>> s = "test01#testing.com; test02#testing.com; test03#testing1.com"
>>> [(e.strip() + '\n') for e in s.split(';')]
['test01#testing.com\n', 'test02#testing.com\n', 'test03#testing1.com\n']
You can use split to split the addresses in multiple lines, strip to remove commas and semi-colons, and join to rearrange them.
>>> s = 'test01#testing.com; test02#testing.com; test03#testing1.com; test04#testing2.com,'
>>> print('\n'.join(m.strip(',;') for m in s.split())))
test01#testing.com
test02#testing.com
test03#testing1.com
test04#testing2.com
How about using regex:
import re
elist = re.findall(r'([^;,]+)', long_list)
print "".join("\n", elist)
If you just want to maintain them as a single multiline string, a simple string substitution will do the trick:
long_list.replace('; ', '\n')
A more flexible solution could use regex:
import re
re.sub(r'\s+;\s+', '\n')