Automatically translated.View original post

🪄 Basic Python: Simple String Makeover with Method! 💖

Let's learn Basic Python Modification Strings! Although the String in Python is Immutable, we can easily create a new String from the original String makeover with the Built-in Methods available! ✨

Today, we offer two of the most popular methods used often:

1. Upper ():

• This method will change all letters in String to all UPPERCASE letters!

• Example: "Hello, World!." Upper () will get the result of "HELLO, WORLD!"

2. Lower ():

• This method will change all letters in String to lowercase!

• Example: "Hello, World!." Lower () will get the result of "hello, world!"

These two methods are very useful when we deal with textual information, such as alphabetizing letters before comparison or storage!

Try to use it to write code more easily and faster! Keep going. 🥰

# python # programmer # data # ai # learnonlemon8

2025/11/12 Edited to

... Read moreถ้าคุณกำลังเริ่มเรียน “คำสั่ง Python เบื้องต้น / python base” เรื่อง String เป็นหนึ่งในสิ่งที่เจอบ่อยมาก และสองเมธอดอย่าง `.upper()` กับ `.lower()` คือของที่ควรรู้ตั้งแต่แรก เพราะช่วย “ทำให้ข้อมูลเป็นมาตรฐาน” ก่อนเอาไปเทียบ/ค้นหา/จัดเก็บได้ง่ายขึ้น สิ่งที่มือใหม่มักงงคือ String ใน Python เป็น immutable (แก้ไขของเดิมไม่ได้) ดังนั้นเวลาเราเขียน `text.upper()` หรือ `text.lower()` จริง ๆ แล้ว Python จะ “สร้างสตริงใหม่” ให้เรา ไม่ได้เปลี่ยนค่าเดิมในตัวแปรอัตโนมัติ ตัวอย่างที่ชอบใช้จำง่าย ๆ: - `"Hello, World!".upper()` จะได้ `"HELLO, WORLD!"` - `"Hello, World!".lower()` จะได้ `"hello, world!"` ถ้าต้องการเก็บผลลัพธ์ไว้ใช้ต่อ ต้องกำหนดตัวแปรใหม่หรือทับตัวเดิม เช่น `text = text.lower()` ทริคที่ใช้งานจริง (เจอบ่อยมากในงานข้อมูล) 1) ทำให้เทียบข้อความได้โดยไม่สนตัวพิมพ์ใหญ่-เล็ก สมมติรับค่าจากผู้ใช้ว่าเขาพิมพ์ “YES”, “Yes”, “yes” ปนกัน เรามักแปลงให้เป็นรูปแบบเดียวก่อน: - ใช้ `answer = answer.lower()` แล้วค่อยเช็ก `if answer == "yes":` แบบนี้ลดบั๊กจากการเทียบสตริงผิดรูปแบบได้เยอะมาก 2) ใช้กับการค้นหา/เช็กคีย์เวิร์ด ถ้าอยากเช็กว่าข้อความมีคำว่า hello ไหม โดยไม่สนเคส: - แปลงทั้งข้อความเป็น lower ก่อน แล้วค่อยใช้ `in` เช่น “Hello, World!” ก็ยังหาเจอด้วยคำว่า `"hello"` 3) ระวังจุดเล็ก ๆ ที่มือใหม่พลาด - `.upper` กับ `.upper()` ไม่เหมือนกัน ต้องมีวงเล็บ `()` ถึงจะ “เรียกใช้งานเมธอด” - เมธอดพวกนี้จะไม่กระทบตัวอักษรที่ไม่ใช่ a-z เช่น ตัวเลข/สัญลักษณ์ จะอยู่เหมือนเดิม (เช่น comma หรือ ! ใน “Hello, World!”) สรุปคือ ถ้าคุณกำลังปูพื้นฐาน python base ให้คล่อง แนะนำให้ฝึกใช้ `.upper()` และ `.lower()` กับข้อความหลายแบบ แล้วลองเอาไปใช้กับงานง่าย ๆ เช่น รับอินพุตผู้ใช้/ทำความสะอาดข้อมูลก่อนเปรียบเทียบ รับรองว่าโค้ดจะอ่านง่ายขึ้นและพังน้อยลงครับ