xxxxxxxxxx
string= "women life freedom"
splited_list= string.split()
print(splited_list)
xxxxxxxxxx
import re
s = '\n \t this is a string with a lot of whitespace\t'
s = re.sub('\s+', '', s)
xxxxxxxxxx
' hello world! '.strip()
'hello world!'
' hello world! '.lstrip()
'hello world! '
' hello world! '.rstrip()
' hello world!'
xxxxxxxxxx
sentence = ' hello apple'
" ".join(sentence.split())
>>> 'hello apple'
xxxxxxxxxx
s = ' This is a sentence with whitespace. \n'
print('Strip leading whitespace: {}'.format(s.lstrip()))
print('Strip trailing whitespace: {}'.format(s.rstrip()))
print('Strip all whitespace: {}'.format(s.strip()))
# Output
# Strip leading whitespace: This is a sentence with whitespace.
# Strip trailing whitespace: This is a sentence with whitespace.
# Strip all whitespace: This is a sentence with whitespace.
xxxxxxxxxx
>>> ' hello world! '.strip() #remove both
'hello world!'
>>> ' hello world!'.lstrip() #remove leading whitespace
'hello world!'
xxxxxxxxxx
def remove_witespace(data_of_string):
string = ""
for char in data_of_string:
if " " not in char:
string = string + char
return string
print(remove_witespace("python is amazing programming language"))
xxxxxxxxxx
# Define a string with whitespace
original_string = " hello world "
# Remove all whitespace using replace()
stripped_string = original_string.replace(" ", "")
# Print the stripped string
print(stripped_string)