[Python]Input/Output and str Formatting

1. Function:Print

Print a sequence of arguments for a user to read. Multiple arguments are separated by spaces.


a. During execution of a function call, if the end of the function body is reached without executing a return statement, that function call produces a value None.

eg. 

>>> def square_return(num):

                  return num**2

return_answer=square_return(2)    //Nothing happens! Type!

return_answer

4

num(2)-3                      //This is a numeric. 

1


>>> def square_print(num):

          print "The square answer is" num**2

print_answer=print_answer(2)

The square answer is 4

num(2)-3   //This is not a numeric.

Error    

Hint: Remenber that the print funxtion only prints a value onto the screen. A function that does not have a return statement returns None.


2. Function: Input

input: get a string from the user.

  The argument to input is called a prompt.

  The functin pauses until the user types a newline.

  The return value is the string the user typed.

eg.

>>> input("What is your name?")
What is your name?Jen
'Jen'
>>> name = input("What is your name? ")
What is your name? Jen
>>> name
'Jen'
>>> location = input("What is your location? ")
What is your location? Toronto
>>> name
'Jen'
>>> location
'Toronto'
>>> print(name, "lives in", location)
Jen lives in Toronto


Triple-quoted strings

We have used single- and double- quotes to represent strings. The third string format uses triple-quotes and a triple-quoted string cab span multiple lines. For example:

>>> print(''' How
are
you?''')
How
are
you?


Escape Sequences

Python has a special character called an escape character:\. When the escape character is used in a string, the character. When the escape character is used in a string, the character following the escape character is treated differently from normal. The escape character together with the character that follows it is an escape sequence. 

Escape SequenceNameExampleOutput
\nnewline (ASCII linefeed - LF)print('''How
are
you?''')
How
are
you?
\ttab (ASCII horizontal tab - TAB)print('3\t4\t5')
3	4	5
\\backslash (\)
print('\\')
\
\'single quote (')
print('don\'t')
don't
\"double quote (")
print("He says, \"hi\".")
He says, "hi".




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值