Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
As a French user of Python 2.7, I'm trying to properly print strings containing accents such as "é", "è", "à", etc. in the Python console.
I already know the trick of using u before the explicit value of a string, such as :
print(u'Université')
which properly prints the last character.
Now, my question is: how can I do the same for a string that is stored as a variable?
Indeed, I know that I could do the following:
mystring = u'Université'
print(mystring)
but the problem is that the value of mystring is bound to be passed into a SQL query (using psycopg2), and therefore I can't afford to store the u inside the value of mystring.
so how could I do something like
"print the unicode value of mystring" ?
The u sigil is not part of the value, it's just a type indicator. To convert a string into a Unicode string, you need to know the encoding.
unicodestring = mystring.decode('utf-8') # or 'latin-1' or ... whatever
and to print it you typically (in Python 2) need to convert back to whatever the system accepts on the output filehandle:
print(unicodestring.encode('utf-8')) # or 'latin-1' or ... whatever
Python 3 clarifies (though not directly simplifies) the situation by keeping Unicode strings and (what is now called) bytes objects separate.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
my_variable_name = str("John")
print(my_variable_name)
This is the code for example, now if I happen to add the double quotes around "my_variable_name" in this statement:
print(my_variable_name)
It just simply display whatever is written inside the "" but if I don't add it'll print "john". Now what I think the reason is because when compiler find something inside "" that tells it to display whatever datatype it is while without "" it just displays the stored or u can say assigned value . I know its easy (basis) but I do this like all the time and my knowledge about this problem never satisfies me
The problem has nothing to do with print function.
"my_variable_name" is a string literal. Single or triple quotes could also be used.
my_variable_name is a reference to a previously defined variable of that name. The type of the variable's value could be anything.
You can print any object, and it'll return the str() representation of it.
Unrelated, you don't need str() function to define a string literal.
when compiler find something inside "" that tells it to display whatever datatype it is
Python is an interpreted language, it's not the compiler doing this. The datatype of anything enclosed in quotes is always a string
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How would I replace characters in a string for certain indices in Python?
For example, I have version = "00.00.00" and need to change each of the 0s to a different value, say 3, to look like "33.33.33". Also, would this be possible if I had a variable storing this value. If I have vnumber = "3", would I be able to get the same output by using the variable? I'm sure replace() is a good function to use for this, but I'm not sure about syntax.
From an interactive session, you could type:
>>> help(str.replace)
But to answer the question most directly:
vnumber = '3'
newversion = version.replace('0', vnumber)
Is probably what you want to do.
Your guess about str.replace was right. It takes to arguments, the first is the string to be found in the original string, and the second is the string to replace the found occurrences of the first argument with. Code could be like this:
vnumber = "3"
version = "00.00.00"
newversion = version.replace("0", vnumber)
print(newversion)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a list like following:
\xeb\x1f\x5e\x31\xdb\x88\x5e\x07\x89\x76\x08\x89\x5e\x0c\x8d\x1e\x8d\x4e\x08\x8d\x56\x0c\x31\xc0\xb8\x0b\x00\x00\x00\xcd\x80\x31\xf6\xe8\xdc\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x41\x42\x42\x42\x42\x43\x43\x43\x43
I want to find \x00 and \xcd\x80 in the string and print it in highlight style. (for example with red color.). How can I do that?
If the string s is unescaped (the \ are real, characters), you can for instance use:
print(s.replace(r'\x00','\x1b[31m\\x00\x1b[0m') \
.replace(r'\xcd\x80','\x1b[31m\\xcd\\x80\x1b[0m'))
What we do here is look for the raw string r'\x00' and replace it by '\x1b[31m\\\x00\x1b[0m'. This means we prepend it with '\x1b[31m', the ANSI terminal escape code for red foreground and append it with '\x1b[0m', the ANSI terminal escape code for dropping markup.
If I run this code with your string on my console, I get:
Now this is of course not very convenient. So you can use:
def print_highlight(s,markers=(r'\x00',r'\xcd\x80')):
for marker in markers:
s = s.replace(marker,'\x1b[31m%s\x1b[0m'%marker)
print(s)
So now you can give it the string s, together with a list of string fragments you want to highlight. For example:
print_highlight(string,(r'\x31',))
will highlight all \x31 parts.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This question is not about usage of int function, but rather how it is done internally.
Because source code is in C I don't understand what is going on there.
Maybe someone can explain how Python convert string "123" to integer 123.
What operations are performed for it?
https://github.com/python/cpython/blob/2d305e1c46abfcd609bf8b2dff8d2065e6af8ab2/Objects/longobject.c#L2075-L2366 contains the implementation you're looking for. While understanding the C is useful, there is a large comment in the middle (starting on line 2132) that explains much of the approach.
When converting a python string to an int, e.g. a = int("123",10), (convert the string "123" to an integer in base 10) a C function is is called.
First, it checks that the given counting base base is >= 2 and <=36, or 0. (Error otherwise)
Next, it ignores all leading spaces. (so that " 123" = "123"),
and check if the number is marked as positive '+', or negative '-'
When the base is 0, it checks if the string starts with '0x','0o', '0b', '0', and sets the base respectively (hexadecimal, octal, binary, decimal).
Note that if no base is given, then the default base is 10 (Decimal).
It then proceeds to turning the character array into a number, using the algorithm described in the code comment at the link posted by Paul Kehrer
Trailing spaces are also ignored, and Errors are raised if needed- for example if there's a space in the middle of the string, followed by a number, or if there's a non-number character.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Basically what I'm asking is, what's the most direct way to convert any integer between 0 and 255 into it's hexadecimal, escaped equivalent? One that I mean will function correctly if wrapped in a write() function (which means '\x56' writes 'V' and not literally '\x56'.
That's what the chr function is for.
f.write(chr(0x56))
Speaking of hexadecimal escaped equivalents isn't really relevant in this context - every character has a hexadecimal equivalent, but in expressing a string the characters that can be expressed as a single simple character are simply output as the character.