Why is it printing an # instead of \100? [duplicate] - python

This question already has answers here:
Why do numbers in a string become "x0n" when a backslash precedes them?
(2 answers)
the reason: python string assignments accidentally change '\b' into '\x08' and '\a' into '\x07', why Python did this?
(2 answers)
Closed 7 years ago.
I have the following program:
x = 0
while x <= 10:
print(x, '\10')
x = x + 1
It then prints:
0 #
1 #
2 #
3 #
4 #
5 #
6 #
7 #
8 #
9 #
Instead of:
1 \ 10, 2 \ 10 And so on...
Why is the program doing this?

You're escaping the 10 with a \ symbol and python is interpreting \10 as a code for the # symbol instead. You can fix this by either placing an r character as a prefix for the string or by escaping the backslash with another one.
Fix:
r'\10' #Raw string
Or:
'\\10'

Related

How to put a str into another str, but on a specific digit in the other str? [duplicate]

This question already has answers here:
Insert some string into given string at given index [duplicate]
(9 answers)
Closed last year.
(Example:)
a = "-"
b = "testtest"
I want to put the a = "-" after 4 digits of the bstring (so between the first and second "test").
So it should be always after the first 4 digits (if the b-str is longer, it shouldn't just go at the first 4th digit.
Apply slicing in a string
index = 4
a = '-'
b = 'testtest'
c = b[:index] + a + b[index:]
a = "-"
b = "testtest"
c = b[:4]+a+b[4:]
print (c)

Python assign a variable use if condition [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 4 years ago.
I want to assign a variable when if statement is true. What's wrong with this assignment?
b = 1 if 3 > 2
File "<stdin>", line 1
b = 1 if 3 > 2
^
SyntaxError: invalid syntax
You need an else in case your condition fails.
b = 1 if 3>2 else 0

Split a string's digits every 3rd digit [duplicate]

This question already has answers here:
Add 'decimal-mark' thousands separators to a number
(9 answers)
Closed 6 years ago.
I have this string
s = '4294967296'
I want to split this into
4.294.967.296
Basically I want to insert a dot every 3rd digit. How can I do this? I tried
c = '4294967296'
for x,y in enumerate(c[::-1]):
if x % 2 == 0:
b = c[:x] + '.' + c[:x]
print (b)
But the output was
>>>
42949672.42949672
>>>
You can (ab)use string formatting:
s = '4294967296'
t = format(int(s), ',').replace(',', '.')
# '4.294.967.296'

How to access parameters of sys.argv Python? [duplicate]

This question already has answers here:
How can I iterate over overlapping (current, next) pairs of values from a list?
(12 answers)
Closed 6 years ago.
if given a command like:
python 1.py ab 2 34
How to print the next argument while you are currently sitting on the one before. e.g if x is ab then I want to print 2:
import sys
for x in sys.argv[1:]:
print next element after x
I am unsure what you want to print but this syntactic sugar can help:
for x in sys.argv[1::2]:
print x
This prints every other element. So this will return ab 34
for x in sys.argv[2::2]:
print x
This will return 2
The number after the :: is how many slots in the array.
Edit:
To answer your specific question:
val = 1
for index, x in enumerate(sys.argv[1::2]):
val += 1
print sys.argv[index+val]
The index increases by 1 each time, and the val by 1 too, meaning every loop we skip 2 variables. Hence for something like python yourcode.py a 1 b 2 c 3 d 4 output will be 1 2 3 4

Print new output on same line [duplicate]

This question already has answers here:
Print in one line dynamically [duplicate]
(22 answers)
Closed 6 years ago.
I want to print the looped output to the screen on the same line.
How do I this in the simplest way for Python 3.x
I know this question has been asked for Python 2.7 by using a comma at the end of the line i.e. print I, but I can't find a solution for Python 3.x.
i = 0
while i <10:
i += 1
## print (i) # python 2.7 would be print i,
print (i) # python 2.7 would be 'print i,'
Screen output.
1
2
3
4
5
6
7
8
9
10
What I want to print is:
12345678910
New readers visit this link aswell http://docs.python.org/release/3.0.1/whatsnew/3.0.html
From help(print):
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
You can use the end keyword:
>>> for i in range(1, 11):
... print(i, end='')
...
12345678910>>>
Note that you'll have to print() the final newline yourself. BTW, you won't get "12345678910" in Python 2 with the trailing comma, you'll get 1 2 3 4 5 6 7 8 9 10 instead.
* for python 2.x *
Use a trailing comma to avoid a newline.
print "Hey Guys!",
print "This is how we print on the same line."
The output for the above code snippet would be,
Hey Guys! This is how we print on the same line.
* for python 3.x *
for i in range(10):
print(i, end="<separator>") # <separator> = \n, <space> etc.
The output for the above code snippet would be (when <separator> = " "),
0 1 2 3 4 5 6 7 8 9
Similar to what has been suggested, you can do:
print(i, end=',')
Output: 0,1,2,3,
print("single",end=" ")
print("line")
this will give output
single line
for the question asked use
i = 0
while i <10:
i += 1
print (i,end="")
You can do something such as:
>>> print(''.join(map(str,range(1,11))))
12345678910
>>> for i in range(1, 11):
... print(i, end=' ')
... if i==len(range(1, 11)): print()
...
1 2 3 4 5 6 7 8 9 10
>>>
This is how to do it so that the printing does not run behind the prompt on the next line.
Lets take an example where you want to print numbers from 0 to n in the same line. You can do this with the help of following code.
n=int(raw_input())
i=0
while(i<n):
print i,
i = i+1
At input, n = 5
Output : 0 1 2 3 4

Categories