this blog is for beginner who wants to work around strings
บลอคนี้เหมาะสำหรับผู้ที่กำลังเริ่มศึกษา python, โจทย์ที่นำมานั้นเป็นโจทย์ทั่วไปที่พบได้ใน programming101 —
Example 1: Drawing X
Q. Drawing X shape by using Asterisk (*)
Solution:
- Set up the background in this case 7 X 7
range (0,7) — Starts at 0,1,2,3,4,5,6 - then we plot — you can use pen and paper to get the idea and turn it into code (it works for me tho!)
Example 2: put * at the corner
- Set up the background in this case 5 X 5
- We want to add * to the corner of this square, then we know that the corner
- the coordinate for corners are (0,0),(0,4),(4,0),(4,4) — then for this certain position we put * to it, otherwise we left it as a blank
- line8: every iteration we give its a newline ‘\n’
Example 3: draw a rectangle room
- Set up the background in this case 3 X 5
- the coordinate for square are all points in the first and last row (so, we get ceiling and floor)
- later we need a wall, then we know that the wall is locate at the first and last point of each row
- line8: every iteration we give its a newline ‘\n’
Example 5: how many x existing in string
- line 1: asking user to input text — this case puff the magic dragon lived by the sea
- line2: asking user what character they want to find — this case ‘e’
- line 3–5: the we iterate over string and count number of ‘e’ existing in the string(b)
- print output