This question already has answers here:
How can I print multiple things on the same line, one at a time?
(18 answers)
How to print without a newline or space
(26 answers)
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 1 year ago.
Not sure if this is asked before but: How do you print "searching for x" where "x" is a random integer? I have my code below:
from random import randint
numbers = []
random = randint(1,50)
for i in range(0,10):
numbers.append(randint(1,50))
for j in range(len(numbers)):
print('searching for')
print(random)
print('in')
print(numbers)
break
And this is what happens but I want "searching for __ in [list]" on the same line. Is there a way to do it?
Thanks in advance!
try this:
print(f'searching for {random} in {numbers}')
it requires python 3.6 or up and it is called an f-string
for n in numbers:
print("Searching for {} in {}".format(n, numbers))
Does that answer what you want to do?
Related
This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 5 days ago.
I'd like to print at same line for print statement inside a for loop using end= parameter. not sure which end parameter i can use.
For example, in below, for each time's print, only need to change str(i) and str(result), everything is the same.
for i in range(10):
result=i**2
print('iteration is'+str(i)+' with result of '+str(result))
Thanks
Use an empty end parameter and go back the length of the previous print
L=0
for i in range(3):
result=i**2
my_str = 'iteration is'+str(i)+' with result of '+str(result)
print('\b'*L + my_str, end='')
L= len(my_str)
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
I tried to create a program which returns the number of times a certain string occurs in the main string.
main_string="ABCDCDC"
find_string="CDC"
print(main_string.count(find_string))
Output=1
....
But there are 2 CDC. Is there any other ways to solve?
Try using regex:
print(len(re.findall(fr"(?={find_string})", main_string)))
Or try using this list comprehension:
x = len(find_string)
print(len([main_string[i:x + i] for i in range(len(main_string)) if main_string[i:x + i] == find_string]))
Both codes output:
2
This question already has answers here:
How can I print multiple things on the same line, one at a time?
(18 answers)
Closed 3 years ago.
I want to know how to print output in a single line
I want to print it like: 1234
instead of
1
2
3
4
Code:
# n = (get in from user)
i=1
while (i<=n):
print (i,)
i +=1
This may help :
For Python 3:
print(i,end='')
For Python 2:
print(i),
This question already has answers here:
How can I print multiple things on the same line, one at a time?
(18 answers)
Print in one line dynamically [duplicate]
(22 answers)
Closed 3 years ago.
I have a program that has to only print data onto one line.
What can I do to do this.
for i in range(10):
print(i)
Is it possible to print all of this on one line so it prints 0, erases the line, print 2, erases, etc..?
Use print(i,end="\r") to return to the start of the line and overwrite.
for i in range(10):
print(i,end=" ")
this is easiest way to print in one line.
in python 2.x:
from __future__ import print_function
for i in range(10):
print (i, end="")
in python 3.x
for i in range(10):
print (i, end="")
For this specific usecase you can do something like this:
print(*range(10))
And to update each character on the line you will need to use '\r' or the return character, that returns the position of the cursor to the beginning of the line. However, you need to be sure you count in the length of the strings you are printing, otherwise you will be overwriting only part of the string. A full proof solution will be:
import time
maxlen = 0
for i in range(12,-1,-1):
if len(str(i))>maxlen:
maxlen = len(str(i))
print(f'\r{str(i): <{maxlen}}',end = '')
time.sleep(2)
print('')
time part is added so that you can view the change. maxlen computes the maximum length string you are going to print and formats the string accordingly. Note: I have used f'strings, hence it would only work for Python 3.x
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
How String.count() works? [duplicate]
(2 answers)
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
I'm having fun with some challenges and one of them makes me count substrings in a string. I have a problem specifically with "banana":
str = "banana"
print(str.count("ana"))
This should return 2 because "ana" appears two times:
b a n a n a
a n a
a n a
But str.count("ana") returns only 1. I've also tried with regexp:
import re
str = "banana"
print(len(re.findall("ana", str)))
But it also returns 1. Am I missing something?
thank you!
Yes, you are missing something.
str.count(): Return the number of (non-overlapping) occurrences of substring sub in string s