Programming LanguagePython

การจัดการกับ String บนภาษาโปรแกรม Python

บทเรียนการจัดการเกี่ยวกับอักขระ หรือประโยคต่างๆที่เป็น Data Type ประเภท String ด้วยภาษา Python สำหรับผู้เริ่มต้นพัฒนาโปรแกรม เพื่อทำความเข้าใจจากภาษา Python

ในบทเรียนนี้เราจะจัดการกับ Data Type ของตัวแปรประเภท String โดยใช้ตัวอย่างต่อไปนี้

str = "Life is not a problem to be solved, but a reality to be experienced. , Soren Kierkegaard"
print (str)

หากทำการบันทึกไฟล์เป็น Tutorial4.py แล้วกด F5 เพื่อ Run คำสั่งแล้วจะได้ผลลัพธ์เรียบง่ายดังนี้

python-4-1

เป็นการ Print ค่าประโยคที่อยู่ภายใน “-” ออกมาทั้งหมด คราวนี้จะเป็นการใช้งานคำสั่งที่เกี่ยวกับตัวแปรประเภท String

str1 = "Life is not a problem to be solved, but a reality to be experienced. , Soren Kierkegaard"
str2 = "Mr. Smith"
newWord = str1.replace("Soren Kierkegaard",str2)
print (":",newWord)

เป็นการเก็บตัวแปร str1 ด้วยประโยคเมื่อครู่ และเพิ่มตัวแปร str2 มาเก็บชื่อของผู้แต่งประโยคนั่นคือ “Mr. Smith” เราจะทำการใช้ฟังก์ชัน String.Replace() เป็นการ แทนค่า String บางคำในประโยคให้เป็น คำใหมแทนที่เข้าไป เก็บลงตัวแปร newWord

ผลลัพธ์คือ

python-4-2

เราสามารถใช้วิธีการ Sub String ใน Python ได้ง่ายโดยการเรียกคำสั่งดังนี้

import string
stringWord = "Life is not a problem to be solved, but a reality to be experienced."
print (stringWord[0], end="")
print()
print (stringWord[0:11], end="")

ผลลัพธ์

python-4-5

คำสั่งแรกคือเรียก String ตำแหน่งแรกของประโยค คือ “L” อีกคำสั่งคือ

print (stringWord[0], end="")

นั่นคือการเรียกตำแหน่งที่ 0 ถึงตัวอักษรที่ 11

print (stringWord[0:11], end="")

เป็นการ substring แบบง่ายๆ  ซึ่งเราไม่อยากใช้ String.Replace() เราสามารถใช้คำสั่ง SubString และ Update String ในตำแหน่งที่ต้องการไปแทนก็ได้

เช่น

import string
stringWord = "Life is not a problem to be solved, but a reality to be experienced."
stringNew = "Life is ***"
print (stringWord[0:11], end="")
print()
print ("Result: ", stringWord[0:11]+stringNew, end="")

เป็นการ Sub ตัดแค่ช่วงของ Lift is not ตำแหน่งที่ 0 ถึง 11 ตามคำสั่ง

print (stringWord[0:11], end="")

แล้วทำการ Update String ด้วยประโยคใหม่ที่เก็บจากตัวแปร stringNew คือ “Life is ***” ไปแทนค่าตำแหน่งนั้นเลย

print ("Result: ", stringWord[0:11]+stringNew, end="")

ผลลัพธ์คือ

python-4-6

คำสั่งการนับ String.Len() เป็นการนับ Length ขนาดของ String ใช้คำสั่งต่อไปนี้

import string
stringWord = "Life is not a problem to be solved, but a reality to be experienced."
print  (str(len(stringWord)),  end="")

ผลลัพธ์คือ

python-4-3

การใช้ String.Strip() เป็นการ “ละ” อักขระบางตัวที่ไม่ต้องการแสดงผล เช่นตัวอย่างที่ใช้คือ

import string
stringWord = "Life is not a problem to be solved, but a reality to be experienced."
print (stringWord.strip("."), end="")

สังเกตว่าเราจะเอาตัวอักขระ “.” คือ . ออกไปจากประโยค

python-4-4

 

สุดท้ายของบทนี้น่าจะเป็นการรับค่าตัวแปรจาก Input ทั้งสองค่าเพื่อนำไป ผสมกับ String Format Operator แบบง่ายๆ ตัวอย่างที่จะยกก็น่าจะเป็นคำสั่งชุดนี้ครับ

import string
fullname = input("ชื่อ-นามสกุล: ")
email = input("อีเมล: ")
ages = int(input("อายุ: "))
salary = float(input("เงินเดือน: "))
print("คุณชื่อ %s  อายุ %d ปี เงินเดือน %f สามารถติดต่อคุณได้ที่อีเมล %s" % (fullname,ages,salary,email), end="")

จะเห็นว่ารูปแบบ String Operation ไม่ต่างอะไรกับตระกูลภาษา C/C++ ใช้ % เป็นค่ารับซึ่งเราสามารถเรียกใช้ตามข้อกำหนดดังนี้

Format Symbol Conversion
%c character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase ‘e’)
%E exponential notation (with UPPERcase ‘E’)
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E

ลองทดสอบ Run ผลลัพธ์ดู

python-4-7

จะเห็นว่าการเรียกรับค่า และนำมาแสดงผลกับ String นั้นไม่ยากเท่าไรเลยใช่ไหมครับ ลองเอาไปประยุกต์ใช้ในบทเรียนกันนะครับ

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน