Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธี Reverse String ใน Python

    วิธีที่ 1: Slicing [::-1] (เร็วที่สุด) Halloween[::-1] neewollaH Stride -1 หมายถึงเดินถอยหลังจากต้นจนจบ วิธีที่ 2: ใช้ reversed() + join() (อ่านง่ายกว่า แต่อยช้ากว่า) .join(reversed(Halloween)) neewollaH reversed() คืน iterator ต้อง join เพื่อรวมเป็นสตริง

  2. วิธี Split String ที่ Newlines ใน Python

    1. ใช้ splitlines() some multi line string.splitlines() [some, multi line, string] 2. ใช้ split(\n) some multi line string.split(\n) [some, multi line, string] splitlines() จัดการ \r, \n, \r\n อัตโนมัติ แนะนำให้ใช้วิธีนี้

  3. วิธี Translate Python String ตาม Dictionary ที่กำหนด

    ใช้ translate() ร่วมกับ maketrans() จากโมดูล string ตัวอย่าง from string import maketrans intab = aeiou outtab = 12345 trantab = maketrans(intab, outtab) str = This is a string example....wow!!! print(str.translate(trantab)) ผลลัพธ์ Th3s 3s 1 str3ng 2x1mpl2....w4w!!! ตัวอักษร a→1, e→2, i→3, o→4, u

  4. วิธี Left-pad String ด้วย Zeros ใน Python

    มี 3 วิธีหลัก: 1. ใช้ rjust(width, fillchar) Apollo.rjust(10, 0) 0000Apollo 2. ใช้ % formatting print(%06d % 25) 000025 3. ใช้ zfill() (สร้างขึ้นมาเพื่อ목 đíchนี้) Apollo.zfill(10) 0000Apollo zfill() แนะนำมากที่สุด เพราะ readable และทำงานได้กับ non-numeric strings ด้วย

  5. วิธีแปลง String Representation ของ List เป็น List ใน Python

    ใช้ ast.literal_eval() ประเมินสตริงเป็น Python expression อย่างปลอดภัย ตัวอย่าง fruits = [apple, orange, banana] import ast fruits = ast.literal_eval(fruits) print(fruits[1], fruits[0]) ผลลัพธ์ orange, apple รองรับ: strings, numbers, tuples, lists, dicts, booleans, None

  6. วิธีตัด Punctuation ทั้งหมดออกจาก String ใน Python

    วิธีเร็วที่สุด: str.translate() import string s = string. With. Punctuation? print(s.translate(None, string.punctuation)) ผลลัพธ์ string With Punctuation วิธีอ่านง่ายกว่า: list comprehension import string s = string. With. Punctuation? exclude = set(string.punctuation) s = .join(ch for ch in s if ch

  7. ความหมายของ Prefix 'b' หน้า String Literal ใน Python

    Python 2: prefix b หรือ B ถูก ignore Python 3: Bytes literals ต้องมี prefix b หรือ B สร้าง instance ของ bytes type แทน str ประกอบด้วย ASCII characters เท่านั้น ค่าตัวเลข 128 ขึ้นไปต้อง escape ความแตกต่างชัดเจนใน Python 3 str = ... # Unicode characters (UTF-16/UTF-32) bytes = b... # sequence of o

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:11/451  20-คอมพิวเตอร์/Page Goto:1 5 6 7 8 9 10 11 12 13 14 15 16 17