Python编程基础(中学版)
上QQ阅读APP看书,第一时间看更新

习题

1.尝试在IDLE中输入下面的字符,并观察得到的结果:

(1)"Hello world"。

(2)"Python Crash"。

(3)"Trackback"。

(4)"spam eggs"。

(5)"she said that"。

(6)"in my opinion"。

(7)"let us do it"。

(8)'are you ok?'。

(9)'several lines'。

(10)'import string'。

(11)'http://www.python.org'。

(12)"let's go"。

(13)'she said "I like moon"'。

(14)'he says "Do you have time?"'。

(15)"""I like the moon"""。

(16)"""you are great"""。

(17)"""yes ,i do"""。

(18)"""are you sure?"""。

2.使用str函数来创建下面的字符:

(1)str("2")。

(2)str("do you know")。

(3)str("i love banana")。

(4)str("she is fond of apple")。

(5)str("they go to the hill")。

(6)str("pi is infinite")。

3.使用repr函数来创建下面的字符:

(1)repr("import template")。

(2)repr('can you show me this time?')。

(3)repr("from string")。

(4)repr("it is a good time")。

(5)repr('i love China')。

(6)repr('a gentleman must never show his socks')。

(7)repr("i'm ok")。

(8)repr("you're sure")。

4.使用print函数打印下面的字符:

(1)print('a')。

(2)print(3*4)。

(3)print(4*'b')。

(4)print("enough for you")。

(5)print('Python is groovy')。

(6)print("This text really won't do anything")。

(7)print("""i ate 8 bananas""")。

(8)print('''This courageous Young Lady of Norway''')。

5.尝试使用“+”或者使用print函数来串联下面的多个字符串:

(1)"boom","eek"。

(2)'there was a Young Lady of Norway', 'bottles would be enough'。

(3)'there are', '2', 'types of people'。

(4)'this is the ', 'left side ', 'of a string'。

(5)"Its fleece "," was white ","as snow"。

(6)'watch that', ' comma', ' at the end'。

(7)'I had this ', 'thing', ' that you could type up right.'。

6.使用字符串的find函数来查找特定字符串的位置:

(1)在字符串"eggs"中查找"e"。

(2)在字符串"eggs"中查找"g"。

(3)在字符串"eggs"中查找"m"。

(4)在字符串"compute the percentage of the hour that"中查找"of"。

(5)在字符串"principle is not defined"中查找"we"。

7.将下面的字符串转换成小写和大写形式:

(1)"Enough for you"。

(2)'a gentleman MUST never show his socks'。

(3)'there was a Young Lady of Norway'。

(4)'http://www.python.org'。

(5)'can you show me this time?'。

(6)"Python Crash"。

(7)"she is fond of apple"。

8.按下面的要求对字符串进行替换:

(1)"Its fleece "将前面字符串中的"Its"替换成"we"。

(2)"""i ate 8 bananas"""将前面字符串中的"i"替换成"They"。

(3)"you're sure"将前面字符串中的"sure"替换成"OK"。

(4)"import template"将前面字符串中的"template"替换成"time"。

(5)'watch that'将前面字符串中的"atc"替换成"i"。

(6)' that you could type up right.'将前面字符串中的"type"替换成"write"。

(7)'a gentleman MUST never show his socks'将前面字符串中的"MUST"替换成"need"。

9.将下面的字符串根据要求进行划分:

(1)"Enough for you"根据空格来划分字符串。

(2)'a gentleman! MUST never show !his socks'根据"!"来划分字符串。

(3)'there! was a @Young @Lady of Norway'根据"!"来划分字符串。

(4)'http://www.python.org'根据"."来划分字符串。

(5)'can you @show me this@ time?'根据"@"来划分字符串。

(6)"Python &&Crash"根据"&"来划分字符串。

(7)"she #is fond #of apple"根据"#"来划分字符串。

10.尝试将下面字符串的前后空格去除然后在IDLE显示屏上打印出来:

(1)" boom eek "。

(2)' there was a Young Lady of Norway bottles would be enough'。

(3)' there are types of people'。

(4)'this is the left side ', 'of a string '。

(5)"Its fleece was white as snow"。

(6)' watch that comma at the end'。

(7)'I had this thing that you could type up right.'。

11.在实际生活中,我们可能会用到不同形式的货币表示,尝试将下面的数值分别用货币形式和科学计数法形式输出:

(1)1000000。

(2)2340000。

(3)845200022200。

(4)90222100032000000。

(5)4220。

(6)33330020002。

(7)3349947766222。

12.尝试将下面的小数按一定的要求打印出来:

(1)0.345 格式:百分号形式。

(2)4.3325 格式:浮点数,保留8位小数。

(3)123.4566 格式:浮点数,一共9位数,保留5位小数。

(4)882.223333 格式:浮点数,一共9位数,保留5位小数。

(5)29302.3008503 格式:浮点数,一共10位数,保留5位小数。

(6)0.126 格式:百分号形式。

(7)16.325 格式:浮点数,保留5位小数。

13.列举常见的转义字符以及它们的用途。

14.和同学们讨论,在什么情况下使用字符串来处理数据会比较方便?