小白學Python ——day5

老師授課內容:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章