I would like to extract one element from list in python.
For example:
a = [0,1,2,3]
b = ['0','1','2','3']
a1 = a[0]
b1 = b[0]
print(a1)
print(b1)
As expected, the code print 0 for both a1 and b1.
However I would like to get '0' for both a1 and b1 with single quotation mark instead of 0.
Any idea or help would be really appreciate.
Thank you,
Issac
Quotation marks are not parts of a value - they are only for distinguish e. g. the number 1 from the string with the only symbol 1.
The print() function don't print quotation marks even around strings (unlike an interactive session when you give as input only a variable name or an expression).
So you have manually put them into the print() function, e. g.
print("'" + str(a1) + "'")
print("'" + b1 + "'")
or
print("'{}'".format(a1))
print("'{}'".format(b1))
or - in Python 3.6+ -
print(f"'{a1}'")
print(f"'{b1}'")
Normally, Python will print a string without quotes. That's standard in almost all programming languages.
However, Python does let you print a string with the quotes, you just need to tell it to print the representation of the string. One way to do that is with the !r formatting directive.
The items in a are integers, if you want them printed as strings you need to convert them to strings.
a = [0, 1, 2, 3]
b = ['0','1','2','3']
a1 = a[0]
b1 = b[0]
print('{!r}'.format(str(a1)))
print('{!r}'.format(b1))
output
'0'
'0'
You can read about the representation of a string in the official tutorial.
There are other ways to see the representation of a string. For example, you can call the built-in repr function:
print(repr(b1))
Bear in mind that if a string already contains single-quote chars then its representation will be enclosed in double-quotes:
s = "A 'test' string"
print(repr(s))
output
"A 'test' string"
And if the string contains a mixture of quotes then the representation will revert to using single quotes, and using backslash escapes to denote single quotes:
s = "A 'test' \"string\""
print(repr(s))
output
'A \'test\' "string"'
So if for some reason you don't want that behaviour, then you will need to print the quote marks explicitly:
s = "A 'test' string"
print("'{}'".format(s))
output
'A 'test' string'
well,
>>> print(repr(str(a1)))
'0'
>>> print(repr(str(b1)))
'0'
will do, but as commented by others I am unsure about your intentions.
I searched online and found Why are some Python strings are printed with quotes and some are printed without quotes?
I think if you print as follows, it will work as you wish:
print(repr(a1))
print(repr(b1))
Related
I can enter an octal value of 'up to 3 characters' in a string.
Is there any way to enter an octal value of only 1 character?
For instance.
If I want to print \0 followed by "Hello", I can do:
"\0Hello"
but if I want to print \0 followed by "12345" I can't do
"\012345"
instead I have to do
"\00012345"
This can, in very obscure scenarios, lead to inconsistent behaviour.
def parseAsString(characters):
output = ['H','I''!','\\','0'] + characters
print("".join(output).encode().decode('unicode_escape'));
parseAsString(['Y','O','U'])
#Output:
#>HI! YOU
parseAsString(['1','2','3'])
#Output:
#>HI!
#>3
The answer to this is, when you're dealing with \0, to either.
Always remember to explicitly use \000 or \x00, this may not be possible if your raw text is coming from another source.
When dealing with raw strings AND concatenating them, always decode each constituent part first, then concatenate them last, not the other way around.
For instance the parser will do this for you if you concatenate strings together:
"\0" + "Hello"
and
"\0" + "12345"
Both work consistently as expected., because "\0" is converted to "\x00" before being concatenated with the rest of the string.
Or, in the more obscure scenario:
def safeParseAsString(characters):
output = "".join(['H','I''!','\\','0']).encode().decode('unicode_escape')
output +="".join(characters).encode().decode('unicode_escape')
print(output)
safeParseAsString(['Y','O','U'])
#Output:
#>HI! YOU
safeParseAsString(['1','2','3'])
#Output:
#>HI! 123
In python when I try this :-
ac = "Pearl Riverb-Vaccines"
b = ac.strip("-Vaccines")
b = b.strip()
print(b)
The output is :- Pearl Riverb
But when I try this :-
ac = "Pearl Rivera-Vaccines"
b = ac.strip("-Vaccines")
b = b.strip()
print(b)
The output is :- Pearl River
So why is the 'a' missing in the second code?
I have tried every other letter and it is printing but what is the problem with letter 'a' ?
strip() does not respect count or order when it removes characters from the end of your string. The argument you passed it, "-Vaccines", contains an "a", so it will remove the "a" from "Rivera". It does not matter that it already removed an "a" from "Vaccines" and it does not matter that it doesn't come between a V and a c.
Consider another example:
>>> "abcXqrqqqrrrqrqrqrqrqqrr".strip("qr")
'abcX'
Many qs and rs are removed here, even though the argument to strip contains only one of each.
In general, strip is not suitable for removing a static number of characters from the end of a string. One possible alternative is to use regex, which can match a literal character sequence that appears at the end of a string:
>>> import re
>>> ac = "Pearl Rivera-Vaccines"
>>> re.sub("-Vaccines$", "", ac)
'Pearl Rivera'
In his answer, Tom Karzes observes that this approach doesn't readily work on strings that contain characters that have special meanings in a regex. For instance,
>>> import re
>>> s = "foo^bar"
>>> re.sub("^bar$", "", s)
'foo^bar'
^ has a special meaning in regex, so the pattern "^bar$" fails to match the end of the string s. If the string you want to match contains special characters, you should escape it, either manually or with an re.escape call.
>>> import re
>>> s = "foo^bar"
>>> re.sub(r"\^bar$", "", s)
'foo'
>>> re.sub(re.escape("^bar") + "$", "", s)
'foo'
The problem is that the argument to strip isn't used the way you think it is. The argument isn't treated as a sequence of characters, but rather as a set of characters. Any character in the argument string is removed. For example:
"abaca".strip("ac")
Produces:
'b'
since all instances of "a" and "b" have been removed.
If you just want to remove a suffix from a string, you can do something like:
ac = "Pearl Rivera-Vaccines"
s = "-Vaccines"
b = ac
if b.endswith(s):
b = b[:-len(s)]
This will result in b having the value:
'Pearl Rivera'
Note that this will be faster than using the re module. It will also be more flexible, since it will work with any non-empty string (whereas creating a regular expression will require escaping certain characters).
When i assign a windows path as a value in dictionary, the backward slash gets added.
I did try using raw string.
p = "c:\windows\pat.exe"
print p
c:\windows\pat.exe
d = {"p": p}
print d
{'p': 'c:\\windows\\pat.exe'}
Tried it as raw string
d = {"p": r"%s" % p}
print d
{'p': 'c:\\windows\\pat.exe'}
I dont want the backslash to added when assigned to value in dictionary.
This is a mistake that's very common among people new to Python.
TL;DR:
>>> print "c:\windows\pat.exe" == 'c:\\windows\\pat.exe'
True
Explanation:
In the first instance, where you're assigning a value to the string p and then printing p, Python gets the string to print itself and it does so by outputting its literal value. In your example:
>>> p = "c:\windows\pat.exe"
>>> print p
c:\windows\pat.exe
In Python 3, the same:
>>> p = "c:\windows\pat.exe"
>>> print(p)
c:\windows\pat.exe
In the second instance, since you're creating and then printing a dictionary, Python asks the dictionary to print itself. It does so by printing a short Python code representation of itself, since there is no standard simple way of printing a dictionary, like there is for variables with simple types like strings or numbers.
In your example (slightly modified to work by itself):
>>> d = {"p": "c:\windows\pat.exe"}
>>> print d
{'p': 'c:\\windows\\pat.exe'}
So, why does the value of p in the Python code representation have the double backslashes? Because a single backslash in a string literal has an ambiguous meaning. In your example, it just so happens that \w and \p don't have special meanings in Python. However, you've maybe seen things like \n and perhaps \t used in strings to represent a new line or a tab character.
For example:
>>> print "Hello\nworld!"
Hello
world!
So how does Python know when to print a new line and when to print \n literally, when you want to? It doesn't. It just assumes that if the character after the \ doesn't make for a special character, you probably wanted to write a \ and if it is, you wanted to write the special character. If you want to literally write a \, regardless of what follows, you need to follow up the escape character (that's what the \ is called in this context) with another one.
For example:
>>> print "I can see \\n"
I can see \n
That way, there is no ambiguity and Python knows exactly what is intended. You should always write backslashes as double backslashes in normal string literals, instead of relying on luck in avoiding control characters like \n or \t. And that's why Python, when printing its code version of your string "c:\windows\pat.exe", prefers to write it as 'c:\\windows\\pat.exe'. Using single quotes, which are preferred even though double quotes are fine too and using double backslashes.
It's just how it is written in code, "really" your string has single backslashes and the quotes are of course not part of it at all.
If you don't like having to write double backslashes, you can consider using 'raw strings', which is prefixing a string with r or R, telling Python to ignore special characters and take the string exactly as written in code:
>>> print r"This won't have \n a line break"
This won't have \n a line break
But watch out! This doesn't work if you want your last characters in the string to be an odd number of \, for reasons not worth getting into. In that case, you have no other recourse than writing the string with double backslashes:
>>> print r"Too bad\"
File "<stdin>", line 1
print r"Too bad\"
^
SyntaxError: EOL while scanning string literal
>>> print r"Too bad\\"
Too bad\\
>>> print "Too bad\\"
Too bad\
Maybe it is not a problem, because when you print the values (not the whole dictionary) the string will have one backslash
p = "c:\windows\pat.exe"
d = {"p": p}
print (d)
{'p': 'c:\\windows\\pat.exe'}
for i in d:
print("key:", i, " value:", d[i])
Output
{'p': 'c:\\windows\\pat.exe'}
key: p value: c:\windows\pat.exe
>>>
I'd like to specify a string with both line continuation and catenation characters. this is really useful if I'm echoing a bunch of related values. Here is a simple example with only two parameters:
temp = "here is\n"\
+"\t{}\n"\
+"\t{}".format("foo","bar")
print(temp)
here's what I get:
here is
{}
foo
And here is what I expect:
here is
foo
bar
What gives?
You can try something like this :
temp = ("here is\n"
"\t{}\n"
"\t{}".format("foo","bar"))
print(temp)
Or like :
# the \t have been replaced with
# 4 spaces just as an example
temp = '''here is
{}
{}'''.format
print(temp('foo', 'bar'))
vs. what you have:
a = "here is\n"
b = "\t{}\n"
c = "\t{}".format("foo","bar")
print( a + b + c)
str.format is called before your strings are concatenated. Think of it like 1 + 2 * 3, where the multiplication is evaluated before the addition.
Just wrap the whole string in parentheses to indicate that you want the strings concatenated before calling str.format:
temp = ("here is\n"
+ "\t{}\n"
+ "\t{}").format("foo","bar")
Python in effect sees this:
Concatenate the result of
"here is\n"
with the resuslt of
"\t{}\n"
with the result of
"\t{}".format("foo","bar")
You have 3 separate string literals, and only the last one has the str.format() method applied.
Note that the Python interpreter is concatenating the strings at runtime.
You should instead use implicit string literal concatenation. Whenever you place two string literals side by side in an expression with no other operators in between, you get a single string:
"This is a single" " long string, even though there are separate literals"
This is stored with the bytecode as a single constant:
>>> compile('"This is a single" " long string, even though there are separate literals"', '', 'single').co_consts
('This is a single long string, even though there are separate literals', None)
>>> compile('"This is two separate" + " strings added together later"', '', 'single').co_consts
('This is two separate', ' strings added together later', None)
From the String literal concatenation documentation:
Multiple adjacent string or bytes literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, "hello" 'world' is equivalent to "helloworld".
When you use implicit string literal concatenation, any .format() call at the end is applied to that whole, single string.
Next, you don't want to use \ backslash line continuation. Use parentheses instead, it is cleaner:
temp = (
"here is\n"
"\t{}\n"
"\t{}".format("foo","bar"))
This is called implicit line joining.
You might also want to learn about multiline string literals, where you use three quotes at the start and end. Newlines are allowed in such strings and remain part of the value:
temp = """\
here is
\t{}
\t{}""".format("foo","bar")
I used a \ backslash after the opening """ to escape the first newline.
The format function is only being applied to the last string.
temp = "here is\n"\
+"\t{}\n"\
+"\t{}".format("foo","bar")
Is doing this:
temp = "here is\n" + "\t{}\n"\ + "\t{}".format("foo","bar")
The key is that the .format() function is only happening to the last string:
"\t{}".format("foo","bar")
You can obtain the desired result using parentheses:
temp = ("here is\n"\
+"\t{}\n"\
+"\t{}").format("foo","bar")
print(temp)
#here is
# foo
# bar
I am new to Python and need help trying to understand two problems i am getting relating to concatenating strings. I am aware that strings can be added to concatenate each other using + symbol like so.
>>> 'a' + 'b'
'ab'
However, i just recently found out you do not even need to use the + symbol to concatenate strings (by accident/fiddling around), which leads to my first problem to understand - How/why is this possible!?
>>> print 'a' + 'b'
ab
Furthermore, I also understand that the '\n' string produces a 'newline'. But when used in conjunction with my first problem. I get the following.
>>> print '\n' 'a'*7
a
a
a
a
a
a
a
So my second problem arises - "Why do i get 7 new lines of the letter 'a'. In other words, shouldn't the repeater symbol, *, repeat the letter 'a' 7 times!? As follows.
>>> print 'a'*7
aaaaaaa
Please help me clarify what is going on.
When "a" "b" is turned into "ab", this ins't the same as concatenating the strings with +. When the Python source code is being read, adjacent strings are automatically joined for convenience.
This isn't a normal operation, which is why it isn't following the order of operations you expect for + and *.
print '\n' 'a'*7
is actually interpreted the same as
print '\na'*7
and not as
print '\n' + 'a'*7
Python concatenates strings together when you do not separate them with a comma:
>>> print 'a' 'b'
ab
>>> print 'a', 'b'
a b
So you are actually printing '\na' 7 times.
I'm not sure what you mean by "how is it possible". You write a rule: two strings next to each other get concatenated. Then you implement it in the parser. Why? Because it allows you do conveniently do things like this:
re.findall('(<?=(foo))' # The first part of a complicated regexp
'>asdas s' # The next part
'[^asd]' # The last part
)
That way, you can describe just what you're doing.
When you do A * B + C, the computer always does A times B first, then adds C, because multiplication comes before addition.
When you do string concatenation by putting the string literals next to each other, and multiplication, the special string concatenation comes first. This means '\n' 'a' * 7 is the same as ('\n' 'a') * 7, so the string you're repeating is '\na'.
You've probably already realised that relying on the implicit concatenation of adjacent strings is sometimes problematic. Also, concatenating with the + operator is not efficient. It's not noticeable if joining only a few small strings, but it is very noticeable at scale.
Be explicit about it; use ''.join()
print '\n'.join(['a'*7])