Multiple string arguments in Python print function [duplicate] - python

This question already has an answer here:
Python string literal concatenation
(1 answer)
Closed 2 years ago.
print('Hello''World')
>>> HelloWorld
How come this works, when multiple string arguments are not separated by comma?
I found this unusual because, like any other function multiple parameters must be separated with commas.
There should have been a syntax error.

In Python, adjacent string literals are concatenated by default.
s = 'Hello' "world" '''!''' """?"""
This is perfectly fine. I used different quotes for each literal, and even separate them with spaces, and everything is still fine.

Related

Function requires a path. How do I make it into a raw path? [duplicate]

This question already has answers here:
Convert regular Python string to raw string
(12 answers)
Closed 1 year ago.
I wrote a function in Python that takes a file path as an argument. Ideally, I would like to 'concatenate' an r at the beginning to escape the characters, and turn it into r"C:\User\name\location".
I am having trouble finding any solutions- are there any modules to help with this?
You do not require any modifications to the function at all.
def f(path):
...
...
f(r"C:\User\name\location")
The "r" you referred to would be used to form the string that you pass to the function. A string is a string, it does not matter how you form it, but Python offers you different ways of doing so e.g.:
f("C:\\User\\name\\location")
By the time the function is passed the string, the string has already been formed. It now makes no difference how it was formed, only that it has all of the correct characters in all the correct places!

Python console returns single quotes for string? [duplicate]

This question already has answers here:
Understanding difference between Double Quote and Single Quote with __repr__()
(3 answers)
Closed 2 years ago.
Why does the Python console returns single quotes for string literals for all types of quote delimeters?
>> '1'
'1'
>> "1"
'1'
>>"""1"""
'1'
Python makes it convenient for itself to handle objects in a simple manner. If you really need an explicit way of memorizing quotes use literal quote characters.
>>> "\"\"Look, I'm around two qoutes!!!\"\""
'""Look, I\'m around two double qoutes!!!""'

Are there literal strings in Python [duplicate]

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'

\r or \n added in arguments list from Python [duplicate]

This question already has answers here:
How can I remove a trailing newline?
(27 answers)
Closed 6 years ago.
i have some scripts that take as input one or multiple paths as on argument.
The script is run like that: myScript.py D:\Folder1,E:\OtherData\Files
In the script, i split the path arguments in the comma and i read the paths.
The problem is that Python adds a \r in the end of each path for no reason.
So the script tries to read D:\Folder1\r and E:\OtherData\Files\r.
Why is this and how can i solve it?
There are many ways, here is one for example.
Replacing the relevant problematic string with an empty string:
paths= [x.rstrip() for x in paths_list]
The method rstrip() returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).

Python read file - pass variable name [duplicate]

This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 7 years ago.
Very simple question for Python 2:
I am calling specific library/function passing filename with readonly flag:
myfunction(r'/tmp/file.txt')
I wanted to replace it with variable:
filename = '/tmp/file.txt'
myfunction(r????)
How can I call that function?
That is not readonly flag. That means raw string. You use it when you don't want escape sequences inside string to be interpreted (like \n, \t etc.) See https://docs.python.org/2.0/ref/strings.html
For your string, you don't need it since it does not contain any escape sequence. Just omit the leading r.
filename = r'/tmp/file.txt'
myfunction(filename)
in most cases you can use
myfunction(r''+filename)
if you don't want to define the r prefix in your variable. This works with b'' and u'' too.

Categories