This prints the list correctly, but it prints it 7 times. I need to print my list only 2 times. I think the list is printing 7 times because there are 7 strings in the list.
Is there a way to only make it print my "custom" list a certain number of times? Because I want to use all these words, but on different lines, and vary each time they are used.
lyrics = ['Ring', 'Gering', 'ding', 'dingeringeding!', 'Wa', 'pa', 'pow!']
for y in lyrics:
print (lyrics[0] +' ' + (lyrics[2] + ' ') * 3 + lyrics[3])
use range, you have seven elements in your list so you iterate seven times with for y in lyrics:
for _ in range(2): # two iterations, prints two times
print (lyrics[0] +' ' + (lyrics[2] + ' ') * 3 + lyrics[3])
It is probably nicer to use str.format:
for _ in range(2): # two iterations, prints two times
print ("{} {} {}".format(lyrics[0] ,lyrics[2] * 3, lyrics[3] )
Which if you don't mind an extra newline can simply be multiplied:
In [36]: print ("{} {} {}\n".format(lyrics[0] ,lyrics[2] * 3, lyrics[3] )* 2)
Ring dingdingding dingeringeding!
Ring dingdingding dingeringeding!
Related
So i have this code where i'm creating a list with substrings:
string = "|01|12345|TEXT1|TEXT2|"
x = string.count("|")
if string.count('|') == 3:
subst = string.strip('|').split('|')
print(substr)
else:
substr = string.strip('|').split('|')
print(substr)
Outcome:
['01', '12345', 'TEXT1', 'TEXT2']
However, i want to print all the substrings so that the outcome is this:
[LAS|01|G12345|TEXT1|TEXT2|]
I know i can just do:
print("[LAS|" + substr[0] + "|G" + substr[1] + "|" + substr[2] + "|" + substr[3])
But this is hardcoded, what if my the string that i get makes way more substrings? I do not want to use allot of if statements, if the count of ('|') == 4, == 5, == 6 etc.
How do i make sure that what i print contains all the substrings. And has a pipe symbol (|) in between every substring.
Thanks,
print("[LAS|%s|]" % "|".join(substr))
The "|".join(substr) takes all the pieces in substr and joins them with the | separator.
If you need the extra "G", you'll need to treat the first one or two elements separately:
print("[LAS|%s|G%s|]" % (substr[0], "|".join(substr[1:]))
or
print("[LAS|%s|G%s|%s|]" % (substr[0], substr[1], "|".join(substr[2:]))
string = '|01|12345|TEXT1|TEXT2|'
substr = string.strip('|').split('|')
if len(substr) != 3: # If you already have used split better to check the length than counting
substr.insert(0, '[LAS')
substr[2] = 'G' + substr[2]
substr.append("]")
print("|".join(substr)) # [LAS|01|G12345|TEXT1|TEXT2|]
I have a sample input string as follows:
med_str = 'Film-coated tablet + ALpha Chloro, Prolonged-release tablet + ALFU Dioxide'
I want to create a list of strings separated by '+'. OUTPUT expected:
med_str = ['Film-coated tablet', 'ALpha Chloro'], ['Prolonged-release tablet', 'ALFU Dioxide']
There might be cases where there would be only one '+' separated string. Example:
new_str = 'Tablet + DEFLAZo'
OUTPUT expected:
new_str = ['Tablet', 'DEFLAZo']
How do I do this using an if else in python which should take care of all the cases and create a separate list of strings separated by comma whenever there is/isn't one or more than one elements with '+' in the string and separated by comma.
Assuming your string always has a whole number of paris, here's how it could be done:
med_str = 'Film-coated tablet + ALpha Chloro, Prolonged-release tablet + ALFU Dioxide'
cleaned = [s.strip() for s in med_str.replace('+',',').split(',')]
result = [[cleaned[i], cleaned[i+1]] for i in range(0, len(cleaned), 2)]
print(result)
Output:
[['Film-coated tablet', 'ALpha Chloro'], ['Prolonged-release tablet', 'ALFU Dioxide']]
Use First Splinting the (,) if its success it will split list into different list with two string concatenated now just split with (+) to get The Remaining Result
med_str = 'Film-coated tablet + ALpha Chloro, Prolonged-release tablet + ALFU Dioxide'
final=[l.split("+") for l in med_str.split(",")]
print(final)
Try this :
med_str = [i.split(" + ") for i in med_str.split(", ")] # Gives output as list of lists.
or
med_str = tuple(i.split(" + ") for i in med_str.split(", ")) # Gives output as tuple of lists.
I need to make a program in Python 3 which outputs the numbers that are not repeted given a sequence of numbers, I have done this so far:
a = list(map(int,input().split()))
for i in a:
if a.count(i) == 1:
print(i,end=" ")
The problem is, if you type the following sequence "4 3 5 2 5 1 3 5", the output will be "4 2 1 " instead of "4 2 1" ( Which was the expected result). My question is, Is there a way to make the "end" argument not to print an extra space at the end?
Thank you,
In that case you better do not let the print(..) add the spaces, but work with str.join instead:
print(' '.join(str(i) for i in a if a.count(i) == 1))
The boldface part is a generator, it yields the str(..) of all the is in a where a.count(i) == 1. We then join these strings together with the space ' ' as separator.
Here print will still add a new line, but we can use end='' to disable that:
print(' '.join(str(i) for i in a if a.count(i) == 1), end='')
so i have to create a code in which it reads every third letter and it creates a space in between each letter, my code creates the spaces but it also has a space after the last letter, this is my code:
msg = input("Message? ")
length = len(msg)
for i in range (0, length, 3):
x = msg[i]
print(x, end=" ")
My output was:
Message?
I enter:
cxohawalkldflghemwnsegfaeap
I get back
c h a l l e n g e
when the output isn't meant to have the last " " after the e.
I have read by adding print(" ".join(x)) should give me the output i need but when i put it in it just gives me a error. Please and Thank you
In Python, strings are one kind of data structures called sequences. Sequences support slicing, which is a simple and fancy way of doing things like "from nth", "to nth" and "every nth". The syntax is sequence[from_index:to_index:stride]. One does not even a for loop for doing that.ago
We can get every 3th character easily by omitting from_index and to_index, and have stride of 3:
>>> msg = input("Message? ")
cxohawalkldflghemwnsegfaeap
>>> every_3th = msg[::3]
>>> every_3th
'challenge'
Now, we just need to insert spaces after each letter. separator.join(iterable) will join elements from iterable together in order with the given separator in between. A string is an iterable, whose elements are the individiual characters.
Thus we can do:
>>> answer = ' '.join(every_3th)
>>> answer
'c h a l l e n g e'
For the final code we can omit intermediate variables and have still a quite readable two-liner:
>>> msg = input('Message? ')
>>> print(' '.join(msg[::3]))
Try
>>> print " ".join([msg[i] for i in range(0, len(msg), 3)])
'c h a l l e n g e'
I'm finishing up an assignment for my 1035 computer science lab and the last thing I need to do is arrange inputted numbers in a diagonal line.
I've tried things like:
print (\tnum2)
and like this:
print ('\t'num2)
but I can't figure out how to do it. I've looked through my programming book, but have been unable to find an explanation on how to do it.
strings in python can be concatenated using the + sign. For example
print(' ' + str(a))
will give the following output for a=1
1
Notice the single blank space before 1. The function str(a) returns the integer a in string format. This is because print statement can only print strings, not integers.
Also
print(' ' * i)
prints i blank spaces. If i = 10, then 10 blank spaces will be printed.
So, the solution to the question can be:
a = [1,2,3,4,5,6,7,8,9,10]
for i in range(len(a)):
print((' ' * i) + str(a[i]))
Here's a simple example that prints items in a list on a diagonal line:
>>> l = [1,2,3,4,5]
>>> for i in range(len(l)):
... print("\t" * i + str(l[i]))
...
1
2
3
4
5
You can also do it using .format
nome = input("nome:")
a = " "
b = len(nome)
for i in range(b):
print ("{0} {1}".format(a * i, nome[i]))
print ("\n next \n")
c=b
for i in range(b):
print ("{0} {1}".format(a * c, nome[i]))
c = c-1
this give diagonal increasing or decreasing