How to replace a ' from a String in python [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to remove a " ' " character from a python string .
Below code gives a syntax error, How to achieve this task
final = string.replace(old_str, '\'', '')

To replace single quotes
final = old_str.replace("'", "")

str = "this is string example....wow!!! this is really string"
print(str.replace("is", "was"))
Here's an example of how to correctly use the replace function. This replaces all the is's with was's. It's hard to see what you're trying to do but use this as a guide.

You can do this
string = "some ' string"
final = string.replace('\'','')
print(final)
output
some string

Related

How do I use attribute in formatted string? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
When I use it like this, it works fine:
print(f'The current price is: {get_price}')
But not like this (and I want the price, not the whole "get_price"):
print(f'The current price is: {get_price['price']}')
Error:
print(f'The current price is: {get_price['price']}')
^
SyntaxError: f-string: unmatched '['
What is the proper way?
The problem is not formatted strings themselves, its the characters you're using. You cannot make a string 'My name is 'Jack' ' because of the ' characters terminating the string prematurly. F-strings are still strings and will experence the same problem. For this, you would use the double quote. Dothis 'My name is "Jack" '

Line print in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am using Python 3 and Pycharm 2021.3. I want to print "4" on the first line. And "3" on the second line. I wrote code like this:
a="4"
b="3"
print(a\n,b)
It show a Error message. Please tell me the right way to write this. Thanks in Advance.
print(a + "\n" + b)
Or
print(a)
print(b)
Or
print(a, b, sep="\n")
When you want to explicitly add a new line, you should use "\n", like you did. However, you mustn't forget that this is a character, so you need to surround it by " or '.
Next, when you want to concatenate string variables, the standard is the use of +.
Put together, if you want to concatenate three variables, in which one of them is a string, a possible way would be:
final = a + '\n' + b
print(final)

How to remove parentheses and blank spaces from a sentence? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I know the question title is similar to many other questions, but I have also read those answers but didn't work for my case. I have a some strings which are like below:
s = '(ANTENOR)'
s = '(ねぼけ)'
Strings are sometimes in English and sometimes in Japanes. I tried different solutions given in StackOverflow but in my case, those aren't working. For example, I tried the following one, but didn't work in my case:
s = re.sub(r'[()]', '', s)
But not working and returns the same string as the original.
My Output should look like below:
ANTENOR
ねぼけ
Only the text, no brackets, and no parentheses. Any help?
That isn't a classic parenthesis, that is FULLWIDTH LEFT PARENTHESIS.
You can see it using ord. And there isn't even a space, there is only char and it has some space before, in it
# yours
print(ord('(')) # 65288
# classic parenthesis
print(ord('(')) # 40
The solution to remove them, is to copy/paste them in the regex
s = '(ANTENOR)'
s = re.sub(r'[)(]', '', s)
print(f">{s}<") # >ANTENOR<
s = '(ねぼけ)'
s = re.sub(r'[)(]', '', s)
print(f">{s}<") # >ねぼけ<

Error print function python "INVALID: Syntax" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Hello I have an error with the code below. I am trying to print "Bus num: "busnum. busnum is data taken from a excel sheet. Can someone explain to me why this invalid syntax as well as ways to improve to that part of the code?
for busnum,change in busses_in_year[location]:
print('Bus #: 'busnum)
I believe you wanted to use string formatting. See below:
for busnum,change in busses_in_year[location]:
print('Bus #: %d' % busnum)
Or you could simply do print("Bus #:" + str(busnum))"
Either way you can't just stick it onto the end there. You have to add it or format it in.
Or you can just use 'comma'.
print('Bus #: ', busnum)
Instead of
print('Bus #: 'busnum)

finding an amount of variable in a text file by python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a variable which its amount is changing every time. I like to know how can I find the line which contained the amount of my variable.
I am looking something like this:
but in this way it looks for the letter of "A"
how can I write a command which look for its amount("100")?
I tried this before
A = 100
with open ('my_file') as f:
for line in f :
if "A" in line:
print line
Putting quotes around something makes it a String. You want to actually reference the variable which contains your number, i.e. A instead of "A"
A = 100
with open('my_file') as f:
for line in f:
# str() converts an integer into a string for searching.
if str(A) in line:
print line

Categories

Resources