Automatically translated.View original post

🐍 BASIC PYTHON: Let's get to know Strings (text)!

String in Python is not just a group of letters, but has very interesting features! 💡

🧵 String is like Array (Sequence / Sequence).

String is a sequence of letters, which allows us to:

1. Letter access (Indexing):

• Like a normal Array, we can access each letter with an Index starting from 0.

• Example: If a = "Hello," then a [0] gets "H."

2. Loop Read One by One (Looping):

• We can easily use for loop to read each letter in String.

3. Find length (length):

• Use the finished function len () to check the total number of letters in the String (including spaces and special symbols).

• Example: len ("Hello, World!") will get 13.

String is one of the most commonly used types in Python! Understand the basics. Our code will definitely be stronger!

# python # programmer # data # ai # learnonlemon8

2025/11/8 Edited to

... Read moreนอกจากพื้นฐานของ String ที่กล่าวมาแล้ว ใน Python เราสามารถใช้ฟังก์ชันและเมธอดต่างๆ เพื่อจัดการกับ Strings ได้อย่างมีประสิทธิภาพมากขึ้น เช่น การใช้ฟังก์ชัน .upper() หรือ .lower() ในการเปลี่ยนตัวอักษรให้เป็นตัวพิมพ์ใหญ่หรือตัวพิมพ์เล็ก หรือการใช้ .strip() เพื่อลบช่องว่างข้างหน้าและข้างหลังของข้อความ นอกจากนี้ เรายังสามารถใช้ .replace() เพื่อแทนที่ข้อความบางส่วนด้วยข้อความอื่น และ .split() เพื่อแยก String ออกเป็นรายการย่อยโดยใช้ตัวแบ่งเฉพาะ ซึ่งเป็นประโยชน์มากเมื่อเราต้องการจัดการกับข้อมูลจากไฟล์ข้อความหรืออินพุตของผู้ใช้ อีกหนึ่งจุดสำคัญคือการเข้าใจเรื่อง Indexing ซึ่งใน Python สามารถใช้ลบ (negative index) เพื่อเข้าถึงตัวอักษรจากด้านหลังของ String ได้ เช่น a[-1] จะหมายถึงตัวอักษรตัวสุดท้ายของ String ทำให้โค้ดสามารถเขียนได้กระชับและยืดหยุ่นขึ้น ในขณะเดียวกัน การใช้ “slicing” ก็ช่วยให้เราสามารถตัดหรือดึงส่วนย่อยของ String ได้อย่างง่ายดาย เช่น a[1:4] จะดึงตัวอักษรตั้งแต่ตัวที่ 2 ถึง 4 ช่วยให้การจัดการ String มีความคล่องตัวมากขึ้น สำหรับนักพัฒนา Python ทุกคน การเข้าใจการใช้งาน Strings อย่างลึกซึ้งจึงเป็นสิ่งจำเป็น โดยเฉพาะเมื่อต้องทำงานกับข้อมูลข้อความ เช่น การวิเคราะห์ข้อมูล (data analysis) หรือการพัฒนาเว็บไซต์และแอปพลิเคชัน เพราะ String เป็นข้อมูลพื้นฐานที่พบเจอและใช้งานบ่อยที่สุด การเรียนรู้และฝึกฝนการใช้งาน String ใน Python จึงถือเป็นการลงทุนที่คุ้มค่าในการเขียนโค้ดให้มีประสิทธิภาพและสร้างสรรค์มากขึ้น