This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
I cant seem to write something like : "C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
As a string to print or otherwise, i think its something to do with the slashes
There can be 3 ways :
1) Either use r (raw strings) before the actual string :
For example : r"C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
2) Or use // like:
For example : "C:\\Users\\alon4\\Desktop\\Learning Material\\Odyssey\\Lab a2\\Magnetism\data"
3) Use slashes: "C:/Users/alon4/Desktop/Learning Material/Odyssey/Lab a2/Magnetism/data"
(As suggested by Óscar López)
Related
This question already has answers here:
How can I do a dictionary format with f-string in Python 3 .6?
(7 answers)
Closed 11 months ago.
How can I convert this to an f-string?
message = '{status} : {method} : {url}'.format(**call_components)
str.format() is usually fine, but if you really want an f-string then it would look like this:
message = f'{call_components["status"]} : {call_components["method"]} : {call_components["url"]}'
This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed 5 years ago.
In F# there is something called a literal string (not string literal), basically if a string literal is preceded by # then it is interpreted as-is, without any escapes.
For example if you want to write the path of a file in Windows(for an os.walk for example) you would do it like this:
"d:\\projects\\re\\p1\\v1\\pjName\\log\\"
Or you could do this(the F# way):
#"d:\projects\re\p1\v1\pjName\log\"
The second variant looks much more clear and pleasing to the eye. Is there something of the sort in python? The documentation doesn't seem to have anything regarding that.
I am working in Python 3.6.3.
There are: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
You can use r prefix.
https://docs.python.org/2.0/ref/strings.html
TL;DR use little r
myString = r'\n'
This question already has an answer here:
Python: Converting HEX string to bytes
(1 answer)
Closed 2 years ago.
I am using python 3 and try to convert a hex-string to a byte-represented form. So i used the following command:
bytes.fromhex('97ad300414b64c')
I Expected results like this: b'\x97\xad\x30\x04\x14\xb6\x4c'' but got b'\x97\xad0\x04\x14\xb6L'. I am note sure what i am doing wrong, but maybe it is something with the encoding?
As pointed by #user8651755 in the comments, this is due to the fact that some bytes correspond to printable characters. So the answer is: you are doing everything right.
This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 5 years ago.
I want to strip the substring '_pf' from a list of strings. It is working for most of them, but not where there is a p in the part of the string I want to remain. e.g.
In: x = 'tcp_pf'
In: x.strip('_pf')
Out:
'tc'
I would expect the sequence above to give an output of 'tcp'
Why doesn't it? Have i misunderstood the strip function?
you can use:
x = 'tcp_ip'
x.split('_ip')[0]
Output:
'tcp'
You can also use spilt function like below,
x.split('_pf')[0]
It will give you tcp.
This question already has an answer here:
Python 3.4.1 script syntax error, arcpy & [duplicate]
(1 answer)
Closed 7 years ago.
I am very new to Python and am getting this small problem. I am using Python 3.3
There is a variable I declared in my code
file_name = "resource\email\ham\6.txt"
However, when I look for the variable, it appends additional numbers
>>file_name
'resource\\email\\ham\x06.txt'
Is there a reason why it behaves as so? If not, how do I remove those additional characters? Also, why are they there?
Use r raw string:
file_name = r"resource\email\ham\6.txt"
Double \:
file_name = "resource\\email\\ham\\6.txt"
Or /:
file_name = "resource/email/ham/6.txt"
\ has a special meaning in python, it is used to escape characters.