python string of questions

1, two ways to execute Python scripts

1 .python enter the interpreter

1 2.python .py file execution

limux years. /1.py

2, the relationship outlined bits, bytes

8 1 byte. Computer processing in bytes, is stored in bits.

3, brief ascii, unicode, utf - 8, the relationship gbk

Ascii beginning

Then came Unicode

UTF --8    Unicode Compact

ascii is the first standard to exchange information with the United States code, all the uppercase and lowercase letters, symbols used to represent the binary, a total of 256, adding some Latin characters such as, 1bytes represent a character,

Unicode is to unify the world without language, unified by two bytes represent a character, you can express 2 ** 16 = 65556, known as the nations language, features: fast, but a waste of space,

Memory may be used in the process, compatible with the UTF -8 , GBK, the ASCII,

UTF -8 To change this disadvantage of Unicode, English characters a predetermined represented by 1 byte, a Chinese character is represented by 3 bytes.
Characteristics; save space, slow, with the hard disk data transfer, the network data transmission speed as compared to hard disk and network, not manifest, and

gbk Chinese character is encoded using two bytes representative of a character.

4, please write "Li Jie" respectively utf - 8 gbk coding and the median share

utf-­‐8:6

gbk  :4

5, Pyhton single-line and multi-line comments are what?

Single-line comments: #
 
multiline comments: "" " "    ""

6, there are those who declare variables precautions?

Rules defined variables:
    1. variable name only letters, numbers, or underscore any combination of
    the first character 2. The variable name not a number
    3. The following keywords can not be declared variable names:
    [ 'and', 'AS ',' assert ',' break ',' class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',' lambda ',' not ',' or ',' pass', 'print', 'raise', 'return ',' try ',' while ',' with ',' yield ']

7, if what variables n1 = 5, please provide a method of using an int, thereby obtaining the variable minimum number of bits can represent?

bt_length

8, Boolean values ​​What?

 

True , False

String

""    ==>

" "    ==>

数字:

0    ==>

其他    ==>

 

a = "123"

a = int(a)


a = 123
b = str(a)


a = 123
p1 = bool(a)

b="i"
b2 = bool(b)

9、阅读代码,请写出执行结果a = "alex"

  b=a.capitaliz()

  print(a)

  print(b)

请写出输出结果:

alex

Alex

10、写代码,有如下变量,请按照要求实现每个功能name = " aleX"

name = " aleX"
a.移除 name 变量对应的值两边的空格,并输入移除后的内容
  print(name.strip())
b.判断 name 变量对应的值是否以 "al" 开头,并输出结果
  print(name.startswith("al"))
c.判断 name 变量对应的值是否以 "X" 结尾,并输出结果
  print(name.endswith("X"))
d.将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果
  print(name.replace("l","p"))
e.将 name 变量对应的值根据 “l” 分割,并输出结果。
  print(name.split("l"))
  print(name.partition("l"))
f.请问,上一题 e 分割之后得到值是什么类型(可选)?
  字典
g.将 name 变量对应的值变大写,并输出结果
  print(name.upper())
h.将 name 变量对应的值变小写,并输出结果
  print(name.lower())
i.请输出 name 变量对应的值的第 2 个字符?

l = len(name)
r = range(0,l)
for nn in r:
if nn == 1:
print(name[nn])

j.请输出 name 变量对应的值的前 3 个字符?

  print(name[0:3])
k.请输出 name 变量对应的值的后 2 个字符?
  print(name[3:5])
l.请输出 name 变量对应的值中 “e” 所在索引位置?

l = len(name)
r = range(0,l)
for nn in r:
if name[nn] == "e":
print("e的索引位置为:" + str(nn))
m.获取子序列,仅不包含最后一个字符。如: oldboy 则获取 oldbo; root 则获roo
print(name[0:4])

 21、字符串是否可迭代对象?如可以请使用 for 循环每一个元素?

for i in 值:
print(i)
 可迭代对象 == 可以被for循环获取
类,类型
str
              各种方法
             ...
对象,根据str类型创建一个对象。s1
int
            ...
a = 123

22、请用代码实现:

a.利用下划线将列表的每一个元素拼接成字符串,li = "alexericrain"
  rint("_".join(li))
b.利用下划线将列表的每一个元素拼接成字符串,li = ['alex', 'eric', 'rain'] (可选)
  print("_".join(li))

23、Python2 中的 range 和 Python3 中的 range 的区别?

Python2:range   立即创建
xrange  for循环时一个一个创建
Python3:range   for循环时一个一个创建
for i in range (0,100,1) :
    print (i)
for i in range (100,0,-1) :
    print (i)

24、实现一个整数加法计算器:
如:
content = input('请输入内容:') # 如 : 5+9 或 5+ 9 或 5 + 9
v = input(">>>")
v1,v2 = v.split("+")
v1 = int(v1)
v2 = int(v2)
print(v1 + v2)
24、计算用户输入的内容中有几个十进制小数?几个字
如:
content = input('请输入内容:') # 如:asduiaf878123jkjsfd-­‐213928

content = input('请输入内容:')
c1 = 0
c2 = 0
for item in content:
if item.isdecimal():
c1 += 1
elif item.isalpha():
c2 += 1
print("字母数量:" + str(c2) + "\n数字数量:" + str(c1))

26、简述 int 和 9 等数字 以及 str 和 "xxoo" 等字符串的关系?

int = 9
str = "xxoo"
类和对象的关系

27、制作趣味模板程序
需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意现实如:敬爱可亲的 xxx,最喜欢在 xxx 地方干 xxx

v = input("请输入输入名字、地点、爱好并用“+”隔开:")
v1,v2,v3 = v.split("+")
content = "敬爱可亲的{name},最喜欢在{place} 地方干{like}"
c = content.format(name = v1,place = v2,like = v3)
print(c)

28、制作随机验证码,不区分大小写。流程:
-­‐ 用户执行程序
-­‐ 给用户显示需要输入的验证码
-­‐ 用户输入的值
用户输入的值和显示的值相同时现实正确信息;否则继续生成随机验证码继续等待用户输入生成随机验证码代码示例:

def check_code():
    import random
    checkcode = ''
    for i in range(4):
        current = random.randrange(0,4)
        if current != i:
            temp = chr(random.randint(65,90))
        else:
            temp = random.randint(0,9)
        checkcode += str(temp)
    return checkcode
code = check_code()
while True:
    code = check_code()
    print(code)
    v = input("请输入验证码:")
    if v == code:
        print("输入正确!!!")
        break
    else:
        print("请重新输入:")

29、开发敏感词语过滤程序,提示用户输入内容,如果用户输入的内容中包含特殊的字符: 如 "苍老师" "东京热",则将内容替换为 ***

c = input(">>>")
c = c.replace("苍老师","***")
c = c.replace("东京热","***")
print(c)

30、制作表格

循环提示用户输入:用户名、密码、邮箱 (要求用户输入的长度不超过 20 个字符,如果超过则只有前 20 个字符有效) 如果用户输入 q 或 Q 表示不再继续输入,将用户输入的内容以表格形式大隐

s = ""
while True:
    name = input("用户名:")
    if len(name) > 20:
        print("用户名长度不超过20字符,如果超过则只有前20个字符有效")
    if name == "q" or name == "Q":
        break
    pwd = input("密码:")
    if pwd == "q" or pwd == "Q":
        break
    mail = input("邮箱:")
    if mail == "q" or mail == "Q":
        break
    temp = "用户名\t密码\t邮箱\n{0}\t{1}\t{2}\n"
    t = temp.format(name,pwd,mail)
    s = s + t
    print(s.expandtabs(20))

 

Guess you like

Origin www.cnblogs.com/raitorei/p/11618888.html