This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 4 years ago.
str1="This is a Case "
print(str1.swapcase())
print(str1)
I expect the output for both print statements to be swapped as:
"tHIS IS A cASE" but the output for first print is "tHIS IS A cASE" and second print is the original str1.
swapcase doesn't change the string that you call it on, it returns a new string. If you want to change the original string, you have to reassign it with the returned value.
str1 = str1.swapcase()
Related
This question already has answers here:
How do I convert a list into a string with spaces in Python?
(6 answers)
Closed 1 year ago.
I have following elements within a list b['lemma'] within a function:
babicka
Marta
I want to return them as string like:
print(b['lemma'], end=" ")
>> babicka Marta
Help would be appreciated.
Use the string join() method, like this:
return " ".join(b['lemma'])
This question already has answers here:
Check if a string contains a number
(20 answers)
Closed 2 years ago.
I have a working function that takes an input as string and returns a string. However, I want my function to return an empty string ("") if the input contains any number in whatever position in the string.
For example :
>>> function("hello")
works fine
>>> function("hello1")
should return ""
The main thing you need for that is the method "isdigit()" that returns True if the character is a digit.
For example:
yourstring="hel4lo3"
for char in yourstring:
if char.isdigit():
print(char)
Will output 4 and 3.
I think it is a good exercise for you trying to do your code from that!
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 6 years ago.
I have a string S = 'spam'
When I use the method replace as S.replace('pa', 'xx')
S.replace('pa', 'xx')
The output I get is -
Out[1044]: "sxxm's"
Why then are the python strings known to be immutable ?
S = 'spam'
S.replace('pa', 'xx')
print S
You will get the same string 'spam'
You are not saving the return value.
Snew = S.replace('pa', 'xx')
should work
This question already has answers here:
In the Python interpreter, how do you return a value without single quotes around it?
(2 answers)
Closed 8 years ago.
In [4]: {'a': "hello"}['a']
Out[4]: 'hello'
I want the output to instead be:
Out[4]: hello
What's the most elegant way to achieve this?
I am also using repr().rjust() later on to print a list of dictionaries into a neatly formatted table, and I notice the repr function also adds quotes which is annoying:
def print_dictionary(d): print repr(d['a']).rjust(10)
print_dictionary({'a':'hello'})
yields:
'hello'
when I want:
hello
Just drop the repr:
def print_dictionary(d): print (d['a']).rjust(10)
This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(31 answers)
Closed 8 years ago.
I want to print all the characters in a string as a list but for each character to be printed once even if recurring. So far I have:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
symbolsx.append(i)
This prints every character, even if the character is repeated.
symbolsx = list(set(symbolsx))
First pass the list to set function to remove duplicates, then reverted that set back to list by passing it to list function.
How about:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
if i not in symbolsx:
symbolsx.append(i)