The ".replace" operator isn't working Python [duplicate] - python

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Why isn't the replace() function working? [duplicate]
(1 answer)
Closed 2 years ago.
I was testing some mechanics out and ran into an issue, the following program should replace the '+' sight to ' + '. The output of this theoretically should be '20 + 20', but in reality, it's '20+20'. I have no idea why.
string = "20+20"
if string.find(" ") == -1:
string.replace("+", " + ")
print(string)

In order for this to work, you need to reassign the string variable with the result of string.replace as the replace function returns the new string.
string = "20+20"
if string.find(" ") == -1:
string = string.replace("+", " + ")
print(string)

Related

Python: Difference between "" and " " when creating a new variable with a empty string?? ( new_variable = " " ) [duplicate]

This question already has answers here:
Why is True returned when checking if an empty string is in another?
(5 answers)
Why empty string is on every string? [duplicate]
(2 answers)
Why does every string contain the empty string?
(2 answers)
Closed last month.
Oke, i've looked into How to check if the string is empty? but it didn't help me. Also chat GPT talks weird if you dig into this question, and the python manual didnt help me either.
Language = python 3.11.1
previous_char = " "
vowels = 'aeiou'
print(previous_char in vowels)
this code evaluates as 'false' and length 1
But if you remove the space between the quotation marks in previous_char
previous_char = ""
vowels = 'aeiou'
print(previous_char in vowels)
this code evaluates as 'true' and length 0
So basically if you ask: is 'nothing' in vowels.. its true??
I don't find this logical, but on the other hand, if it would evaluate to false, it would also be weird.
I started coding 2 weeks ago for fun, i'm 35 years old, so please don't burn me to hard if this is some kind of dumb question.
But i'm a bit stuck in understanding why this is the way it is?
" " is a string containing a single character (a space).
"" is the empty string.

Are python strings can be mutable? [duplicate]

This question already has answers here:
Aren't Python strings immutable? Then why does a + " " + b work?
(22 answers)
How to add a string in a certain position?
(10 answers)
Closed 1 year ago.
As string is immutable,so we can't change the string so how we can insert a character at middle position?
code:
s = "hello world"
s[5] = '-'
But it gives you error as it is immutable.so,how we can resolve this problem?
We know string is immutable,but we can't change values through assignment operator.so we can acheive this through string slicing:
s = s[:5]+'-'+s[6:]
so now s becomes "hello-world".
so this can be done using string slicing.
Yes , the strings in the Python are immutable.
But we can perform concatenate operation on strings.
If we want to modify string like..
S = "Hello World"
S[5] = '-'
It is not possible but we can do this by slicing method
S = S[:5] + '-' + S[6:]
Then the result is
S = "Hello-World"

Python replace with same string not working? [duplicate]

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 2 years ago.
The following code should take strings in for form of #d# and roll a random number based on the dice then replace #d# with #.
For example 1d6 would roll 1 6-sided die and replace 1d6 with, say, a 5.
# Turn dice notation into a number
def parse_dice(string):
# Replace all dice notations #d# with a number #
matches = re.findall("[0-9]+d[0-9]+", string)
for match in matches:
a = match.split("d", 1)[0]
b = match.split("d", 1)[1]
roll = str(roll_dice(int(a), int(b)))
print("String is: " + string + "\nThe match is: " + match + "\nI'll replace it with the roll: " + roll)
string.replace(match, roll)
print("After replacing I got: " + string)
# Parse any leftover math tacked on
number = eval(string)
return number
The print statements are for testing. Here's an example output:
String is: 1d6
The match is: 1d6
I'll replace it with the roll: 2
After replacing I got: 1d6
For some reason, even though the string matches the string I'm replacing EXACTLY it won't replace it so at the end I'm running eval("1d6") instead of eval("5")
replace() returns a copy of the string where all occurrences of a substring is replaced.
You cannot mutate (modify) a string in python. Even if you want badly. Strings are immutable datatype by design, so, please, create a new string. The proper use is
string2 = string.replace(....
well, as Thierry suggested, you can
store that new string in the same variable
string = string.replace(...

Replacing all characters of a given string with a specific single character python with no loops [duplicate]

This question already has answers here:
replace all characters in a string with asterisks
(4 answers)
Closed 4 years ago.
str0 = input("Enter str: ")
str0 = str0.replace(str0, '_')
print(str0)
I need to replace each one of the characters without using loops or conditions
str0 = input("Enter str: ")
str0 = '_'*len(str0)
print(str0)
Should work. The multiplication puts the right amount of _'s so that all of them are replaced.

how do i reverse the letters in a string in python? [duplicate]

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 7 years ago.
i want to reverse the order of characters in a string
so take strings as arguments
and return s new string with letters of the original string
example - "hello" would return "olleh"
All I have gotten to is:
def reverse(" "):
string = input("Give me a word")
x = ord(string)
print(x)
You can do it like this:
"hello"[::-1]
You can read about extended-slices here

Categories