3

Question 1:

word = 'fast'

print '"',word,'" is nice' gives output as " fast " is nice.

How do i get the output "fast" is nice ie I want the spaces to be removed before and after word?

Question 2:

def faultyPrint():
    print 'nice'

print 'Word is', faultyPrint() gives me output as

Word is nice
None

I want to have the output as Word is nice and None removed.

I don't want the output from

print 'Word is'
faultyPrint()

as it gives me output as

Word is
nice

How do I do that without altering the function and keeping the same output format?

3
  • 2
    what's wrong in returning the string "nice" from function? Commented Oct 27, 2012 at 11:36
  • @AshwiniChaudhary: I have to change many other functions Commented Oct 27, 2012 at 11:44
  • Usually it is better to fix the problem properly. Even if it means refactoring other code. Commented Oct 27, 2012 at 11:50

5 Answers 5

9

A more extensible approach would be the following.

For the first part:

word = "fast"
print('"{0}" is nice'.format(word))

(For the brackets: If you pass only one argument, they make no difference and give you python3 compatibility for free in most of the cases)

For more details on this one, see Python String Formatting Syntax (Examples here).

For the second part:

The only way to fix this without patching the function is to not create a newline at the end of your print:

print "Word is", 
faultyPrint()

If you want to stay Python3 upwards compatible, you have to do:

from __future__ import print_function  #put that at the head of your file
print("Word is ", end="")
faultyPrint()

(Notice the (non-obvious) difference: In Python3, you'll need a space at the end of the string)

In general, it would be more appropriate though to return the values to be printed, preferrably as the most appropriate data type (i.e. do not ",".join(foo) a list, return the list and do the join in the outermost function). This serves reusability and separation of logic and presentation.

Sign up to request clarification or add additional context in comments.

Comments

3

Use + rather than , to join the strings

print '"' + word + '"'

In regards to your 2nd question. The function is returning None. So that is what is getting printed.

1 Comment

Thanks a lot. how can i get output for the second part?
2

This is probably what you are looking for:

print 'Word is',     #Notice the trailing comma. This will not print a newline char
faultyPrint()

Comments

2

Q1:

If you use comma, you get automatically extra space between elements you are going to print.

>>> print "A","B","C"
A B C

If you use plus, you don't get extra spaces.

>>> print "A"+"B"+"C"
ABC

Q2:

I'm guessing you want to print some constant string, and then something from function after it, in the same line. This can be done in a few ways:

First way: This is how I would do it, using return value of function:

def faultyPrint():
    return 'nice'

>>> print 'Word is', faultyPrint()  # Prints 'Word is', and return value of function
Word is nice

NOTE: If you don't specify a return value for a function in Python, return value will be None. This is why you got None in your output.

Second way: If you really wan't to avoid writing return statement in your function. (But your function will actually return None in this case too)

def faultyPrint():
    print 'nice'   # prints nice and newline

print 'Word is',   # Adding comma to end of print won't add newline
faultyPrint()      # Normal function call, prints nice and newline

Comments

0
def faultyPrint():
    print 'nice'
    return ""

1 Comment

I dont want the function to be changed:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.