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

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" '

Related

Couldnt print out the Hexadecimal value of the 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 1 year ago.
Improve this question
string = "Karma police, arrest this man, he talks in maths"
byte_string = string.encode("utf-8")
hex_string = byte_string.hex()
print("Hex: ".format(hex_string))
I am trying to print out the hexadecimal value of the string, but the output is empty
ouput-> Hex:
What should I change in order to print out the hexadecimal value?
You forgot placeholder for .format()
string = "Karma police, arrest this man, he talks in maths"
byte_string = string.encode("utf-8")
hex_string = byte_string.hex()
print("Hex: {}".format(hex_string))

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}<") # >ねぼけ<

"starts with" does not work (python regex) [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 3 years ago.
Improve this question
I have this string:
m = 'Film/6702-BRINGING-UP-BABY" da'
and the following regax, that tries to find words that start with 'd':
movie = re.findall(r'^d.*', m)
print(movie)
the result is:
[]
and I can not understand why - there is a match in the string('da'), so why wont it find it?
I an working with pycharm, python 3.6
Using \b as boundary character we can find the words starting with specific character
import re
m = 'Film/6702-BRINGING-UP-BABY" da'
movie = re.findall(r'\bd.*', m)
print(movie)
output
['da']

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

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

Python bytearray.fromhex fails when string has "16" [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 6 years ago.
Improve this question
In Python 2.7, the function bytearray.fromhex(string) gives:
ValueError: non-hexadecimal number found in fromhex() arg at position x
when the string has '16' in it, like in for example:
0200FF001603000E30D03, 0200FF001603004401A03
It's like failing if it was decimal and was '84102' just for having '10', the base, in it.
How can I avoid that error?
There is no problem with 16 existed in the string, the problem is that you try to encode odd length strings - try any valid string with even length and you will see that it is works.

Categories

Resources