小白学Python ——day5

本文详细介绍了Python中常用的字符串处理函数,如ord(), chr(), join(), split(), replace()等,并通过实例演示了如何使用这些函数进行字符串操作。此外,还提供了Python绘图库turtle的实践案例,包括奥运五环和五角星的绘制过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

老师授课内容:

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值