小白学Python ——day5

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaoluwei/article/details/86086382

老师授课内容:

day05-实训5

1、常用函数
字符串函数(非常重要)
 ord() 给一个字符,转化为ascii值
 chr() 给一个ascii值,转化为字符
 join() 将列表里面的字符串按照特定的字符拼接
 ljust(width, 字符) 共多少个,居左对齐
 rjust() 居右对齐
 center() 居中对齐
 zfill()  右对齐,左边0填充

 strip()  默认去除字符串两边的空格,也可以去掉指定字符
 lstrip()    去除左边的空格或者指定字符
 rstrip() 去除右边的空格或者指定字符

 replace() 字符串替换,默认替换所有,可以指定个数替换
 split()    按照指定的字符切割得到一个列表
 splitlines() 按照换行符切割
 find()   字符串查找,从左边找第一个,找到返回下标,找不到返回-1
 rfind()   字符串查找,从右边找第一个,找到返回下标,找不到返回-1

 upper()  小写转大写
 lower()  大写转小写
 capitalize()  首字母大写
 title()    单词首字母大写
 swapcase()   大小写互换

 count()   查找指定字符串出现的次数 
 len()      字符串长度
 startswith()   是不是以某个字符串开头   返回一个bool值
 endswith()   是不是以某个字符串结尾   返回一个bool值

课堂练习:

奥运五环简单的绘制:

import turtle
import random
import time

turtle.screensize(500, 500)

turtle.pensize(10)
turtle.speed(1)
turtle.showturtle()

'''
00  黑色
220 0  红色
-220 0  蓝色
-110 -100 黄色
110 -100 绿色
'''

turtle.pendown()
turtle.circle(100)

def huayuan(color, x, y, r=100):
    turtle.begin_fill()
    turtle.penup()
    turtle.color(color)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.fillcolor(color)
    turtle.circle(r)
    turtle.end_fill()

huayuan('red', 220, 0)
huayuan('blue', -220, 0)
huayuan('yellow', -110, -100)
huayuan('green', 110, -100)

'''
turtle.begin_fill()
turtle.penup()
turtle.color('blue')
turtle.goto(-220, 0)
turtle.pendown()
turtle.fillcolor('blue')
turtle.circle(100)
turtle.end_fill()

turtle.begin_fill()
turtle.penup()
turtle.color('yellow')
turtle.goto(-110, -100)
turtle.pendown()
turtle.fillcolor('yellow')
turtle.circle(100)
turtle.end_fill()

turtle.begin_fill()
turtle.penup()
turtle.color('green')
turtle.goto(110, -100)
turtle.pendown()
turtle.fillcolor('green')
turtle.circle(100)
turtle.end_fill()
'''
# time.sleep(5)
# turtle.reset()
# turtle.forward(200)


turtle.done()

五角星的绘制:

import turtle
import random
import time

turtle.screensize(500, 500)

turtle.pensize(10)
turtle.speed(1)
turtle.showturtle()
turtle.color('red')

turtle.begin_fill()
turtle.fillcolor('red')
turtle.pendown()

for i in range(5):
    turtle.forward(400)
    turtle.right(144)

turtle.end_fill()
turtle.done()

猜你喜欢

转载自blog.csdn.net/zhaoluwei/article/details/86086382