I tried to do raw strings but it gives be errors since it ends in a backslash and it is too stupid to see it is a raw string. I know about doing\\but that seems to just print two actual backslashes instead of one. I even tried doing chr(92) BUT THAT STILL PRINTED \\ FOR SOME REASON. Please if anyone could help it would be great.
You can bypass Python string interpolation logic using chr(chr_number) In this case 92:
>>> li=[chr(92)]
>>> li
['\\'] # that is a SINGLE character of '\'
Then use * to make it any length:
>>> s=chr(92)*3
>>> s
'\\\\\\'
>>> len(s)
3
And it works in f strings as you may want:
>>> f'{chr(92)}'
'\\'
You can use double backslashes. Imagine that second one negates it.
ls = ['\\']
Videos
or use:
print(r'This is \backslash')
But escaping with this \\backslash is recommended.
You have to escape the backslash:
\\
From Python Docs:
The backslash
(\)character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
Also, as @Torxed mentioned in his answer, you can use the prefix r or R:
String literals may optionally be prefixed with a letter
'r'or'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences.
r"Some string with \ backslash"
mid_idx = len(sorted_list)//2 I feel like I know this but forgot
I did there were multiple solutions like using "\" and r"\"" but they both print two backslashes instead of one, and i want to print a single slash only.
There is only one backslash,these options are right, the reason why you see two is because you used it in the shell.
If you used print(xxx) , you will see it renders correctly.
For exsample:
def format_changer_m():
date=input('Enter date in DD/MM/YYYY: ')
x=date.split('/')
return x[1]+"\\"+x[0]+'\\'+x[2]
print(format_changer_m())
Backslashes in strings are used as an escape character. You will need to use "\\" to output a single \ this code will not run be cause you are instead escaping the " so that it is treated as a character within the string. google: escape characters python.
sidenote: you don't need brackets for a return function
When printing a list in python the repr() method is called in the background. This means that print(list_of_strings) is essentially the same thing (besides the newlines) as:
>>> for element in list_of_strings:
... print(element.__repr__())
...
'<'
'>'
'€'
'£'
'$'
'¥'
'¤'
'\\'
In actuality the string stored is '\' it's just represented as '\\'
>>> for element in list_of_strings:
... print(element)
...
<
>
€
£
$
¥
¤
\
If you print out every element individually as above it will show you the literal value as opposed to the represented value.
Yes, the output will always have a double slash, because that is how string objects represent a single slash. And when you print a list, the list __str__ and __repr__ methods use the objects __repr__ methods to build up what is being printed.
But if you print(list_of_strings[-1]) you will see, it is a single slash. Why does this even matter? The key here is to understand the distinction between source code string literals, the way an object is represented, and what the actual value of the string object is.
If it really bothers you, write a function to print the list yourself:
>>> list_of_strings = ['<','>','\u20AC','\u00A3','$','\u00A5','\u00A4','\\']
>>> def print_list(l):
... innards = ','.join(l)
... print(f'[{innards}]')
...
>>> print_list(list_of_strings)
[<,>,€,£,$,¥,¤,\]
>>>
Printing a list object directly should mainly be used for debugging. Whenever you need some particular output formatting, you should be handling that yourself.
The goal: 'apple' -> 'app\e'
This doesn't work:
>>> "apple".replace('l', '\\')
'app\\e'This also doesn't work:
>>> "apple".replace('l', '\')
File "<stdin>", line 1
"apple".replace('l', '\')
^
SyntaxError: EOL while scanning string literalYou need to escape your backslash by preceding it with, yes, another backslash:
print("\\")
The \ character is called an escape character, which interprets the character following it differently. For example, n by itself is simply a letter, but when you precede it with a backslash, it becomes \n, which is the newline character.
As you can probably guess, \ also needs to be escaped so it doesn't function like an escape character. You have to... escape the escape, essentially.
See the documentation for string literals.
A hacky way of printing a backslash that doesn't involve escaping is to pass its character code to chr:
>>> print(chr(92))
\