user3732216
user3732216

Reputation: 1589

For loop in Python In Sentence

Can someone tell me how I can loop through these places and not have it list it down by continue with the string as attached to the string.

world = ["Japan", "China", "Seattle", "Brazil"]

print "This is my story of the following places: " 

for place in world:
    print place

EDIT: I put a comma at the end of the print command however since its a separated list I would prefer if I was able to add a separator. End result I want it to show like:

This is my store of the following places. Japan, China, Seattle, Brazil.

Final Edit: With the following code it drops off a line and I'm trying to find out how to remove the additional line so that it continues on the same line.

world = ["Japan", "China", "Seattle", "Brazil"]


print "This is my story of the following places: ", 

for i in range(0,len(world)):
    if i==(len(world)-1):
        print world[i] + '.'
    else:
        print world[i] +',',

print ". Which has all 

This is the story of the following places I visited. They are China, Japan, Seattle, and Brazil

. Which has all of the great icons fo

Upvotes: 2

Views: 5926

Answers (3)

David Pinos
David Pinos

Reputation: 1

This is program for python that I modified so you can calculate the multiplicative inverse for a whole set from 0 to "m":

#This code was edited by David Pinos from cybersecurity Centennial College. Canada
# This code is contributed by Nikita tiwari and it was taked from https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/#:~:text=To%20find%20multiplicative%20inverse%20of,value%20of%20gcd%20as%201.&text=We%20can%20remove%20the%20second,0%20for%20an%20integer%20y.

# Iterative Python 3 program to find 
# modular inverse using extended 
# Euclid algorithm 
  
# Returns modulo inverse of a with 
# respect to m using extended Euclid 
# Algorithm Assumption: a and m are 
# coprimes, i.e., gcd(a, m) = 1 
def modInverse(a, m) : 
    m0 = m 
    y = 0
    x = 1
  
    if (m == 1) : 
        return 0
  
    while (a > 1) : 
  
        # q is quotient 
        if (m==0):
            return "there is no inverse for this number"
        else:
            q = a // m 
            t = m 
  
            # m is remainder now, process 
            # same as Euclid's algo 
            m = a % m 
            a = t 
            t = y 
  
            # Update x and y 
            y = x - q * y 
            x = t 
    # Make x positive 
    if (x < 0) : 
        x = x + m0 
    return x 
  
  
# Driver program to test above function 

m = 26 #your SET

i=0
while (i<m):
    print("Modular multiplicative inverse of ", i, " in ", m, " is: ", modInverse(i, m))
    i=i+1

Upvotes: 0

logic
logic

Reputation: 1727

You have to add a comma after the print() command to prevent new lines:

In response to your comment:

world = ["Japan", "China", "Seattle", "Brazil"]


print "This is my story of the following places: ", 

for i in range(0,len(world)):
    if i==(len(world)-1):
        print world[i] + '.', #<-- Add another comma
    else:
        print world[i] +',',

This outputs:

This is my story of the following places:  Japan, China, Seattle, Brazil.

I made the if-statement to add 'commas' and a 'period' to make the output look like a list. This would work with ANY size list.

However, take a look at the join() method (suggested by @ILostMySpoon). It is much more efficient.

Upvotes: 1

ILostMySpoon
ILostMySpoon

Reputation: 2409

Use the join() method to take a list of things to join with a given string

print "This is my store of the following places. " + ", ".join(world)

Upvotes: 6

Related Questions