trinidad34
trinidad34

Reputation: 39

Is there a way to concatenate characters in a string so that there are no spaces in between the letters?

So this is it what it would look like

string1 = "This is my string"

output would be:

string1 = "Thisismystring"

Upvotes: 0

Views: 32

Answers (2)

Anaam
Anaam

Reputation: 124

>>> def foo(s: str):
...     return s.replace(' ', '')
...
>>> print(foo("This is my string"))
Thisismystring
>>> def foo(s: str):
...     return ''.join(s.split(' '))
...
>>> print(foo("This is my string"))
Thisismystring

Upvotes: 2

dahiya_boy
dahiya_boy

Reputation: 9503

Try this

import re
string1 = "This is my string" 
output = re.sub(r'\s', '', string1)
# or 
output= string1.replace(" ","")
print(output)

Upvotes: 0

Related Questions