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

    กรณี positive integers เท่านั้น: split แล้ว filter ด้วย isdigit() [int(s) for s in str.split() if s.isdigit()] [23, 11, 2] กรณีทั่วไป (รวมลบ, ทศนิยม): ใช้ regex [float(s) for s in re.findall(r-?\d+\.?\d*, he33.45llo -42 I\m a 32 string 30)] [33.45, -42.0, 32.0, 30.0] Regex -?\d+\.?\d* match ตัวเลข

  2. วิธีแปลง Hex String เป็น Int ใน Python

    Hex string มักมี prefix 0x มี prefix 0x: ใช้ int(string, 0) int(0xfe43, 0) 65091 ค่า 0 บอกให้ฟังก์ชัน detect base เองจาก prefix ไม่มี prefix: ใช้ int(string, 16) int(fe43, 16) 65091

  3. วิธีดึงชื่อ Variable เป็น String ใน Python

    Python ไม่มีวิธีมาตรฐานให้ดึงชื่อตัวแปร เพราะตัวแปรคือ name mapping ไปยัง object เฉพาะ modules, functions, classes เท่านั้นที่มี canonical name (__name__) วิธี ย้อนกลับ หาชื่อตัวแปรจากค่า (reverse lookup): my_var_name my_var ข้อจำกัด: ถ้ามีตัวแปรหลายตัวมีค่าเท่ากัน อาจคืนชื่อตัวไหนตัวหนึ่ง

  4. วิธีเปลี่ยน ID ของ Immutable String ใน Python

    String ใน Python เป็น immutable ไม่สามารถเปลี่ยนแปลงได้หลังสร้าง Python ใช้ string interning สตริงเดียวกันอาจชี้ไปที่หน่วยความจำเดียวกัน ตัวอย่าง interning id(b) 43706848 # id เหมือนกัน แต่ไม่ guarantee ว่าจะ interning ทุกกรณี สตริงที่มี special characters อาจไม่ interning ตัวอย่าง id ต่างกัน id(

  5. ทำไม Backslash ถึงแสดงสองตัวเมื่อ Print ใน Python

    Backslash เป็น escape character ใช้ escape quotes, newline ฯลฯ เวลา print สตริงที่มี backslash จะเห็น 2 ตัว เพราะ backslash ตัวแรก escape backslash ตัวที่สอง (บอก interpreter ว่าตัวนี้เป็น backslash ธรรมดา ไม่ใช่ escape char) ตัวอย่าง print(hello \\ John\s friends) ผลลัพธ์ hello \ Johns friends

  6. วิธี Execute String ที่มี Python Code ใน Python

    exec() ใช้สำหรับ statement (หลายบรรทัดได้) exec(my_code) Hello World! eval() ใช้สำหรับ expression (คืนค่า) eval(my_expression) 8 ⚠️ คำเตือน: ใช้ระมัดระวังมาก อาจก่อให้เกิด security loopholes และ bugs ที่ล้มลึก

  7. วิธีสร้าง Multi-line String ยาวใน Python

    ใช้ triple quotes (สามคู่ของ single/double quotes) ตัวอย่าง multiline_str = My multi-line string print(multiline_str) ผลลัพธ์ My multi-line string หมายเหตุ: ไม่สามารถ interpolate string ได้ ใช้สำหรับ docstrings ด้วย

  8. วิธีเช็ค String ว่างอย่าง Elegan ใน Python

    Empty string เป็น falsy ใช้ not string ตรวจสอบได้เลย ตัวอย่าง string = if not string: print(Empty String!) ผลลัพธ์ Empty String! กรณีมี whitespace ต้องการถือว่าว่าง string = if not string.strip(): print(Empty String!) ผลลัพธ์ Empty String!

  9. วิธีประมวลผล Escape Sequences ใน String Python

    เนื้อหาเหมือน ID 1004108049 (unescape backslash string) วิธีที่ 1: ast.literal_eval() print(ast.literal_eval(a)) Hello, world วิธีที่ 2: decode(string_escape) (Python 2) print(Hello,\\nworld.decode(string_escape)) Hello, world Python 3 ใช้ bytes(s, utf-8).decode(unicode_escape)

  10. ทำไม Substring Slicing Index Out of Range จึงไม่ Error ใน Python

    Slicing นอกขอบเขตลำดับ (sequence) ไม่เกิด error เพราะ: Indexing คืนค่า single item → ไม่มี item ให้คืน → Error Slicing คืนค่า subsequence → ไม่มี item → คืน empty sequence ตัวอย่าง s[5:8] [] แต่ indexing นอกขอบเขต: s[5] IndexError: list index out of range

  11. วิธีรวม String กับตัวเลขใน Python (ซ้ำ)

    บทความนี้ซ้ำกับ ID 1004108048 เนื้อหาเหมือนกันทุกประการ วิธีการ: แปลงตัวเลขเป็นสตริงด้วย str() แล้ว concat ด้วย + print(a + str(b)) string1 Python 2 ใช้ backtick `b` ได้ แต่ Python 3 ไม่รองรับ

  12. วิธีตัด Non-printable Characters ออกจาก String ใน Python

    กรณี ASCII เท่านั้น: ใช้ string.printable กรองตัวอักษร .join(filter(lambda x: x in string.printable, \x01string)) string รองรับ Unicode: ใช้ unicodedata และ regex ตัวอย่างโค้ด import sys, unicodedata, re # สร้าง regex ของ control characters (category Cc) all_chars = (chr(i) for i in range(sys.maxu

  13. วิธี Unescape Backslash-escaped String ใน Python

    มี 2 วิธีหลัก: 1. ใช้ ast.literal_eval() ต้องห่อสตริงด้วย quote เพิ่มอีกชั้น print(ast.literal_eval(a)) Hello, world 2. ใช้ decode(string_escape) (Python 2) print(Hello,\\nworld.decode(string_escape)) Hello, world หมายเหตุ: ใน Python 3 ให้ใช้ bytes(s, utf-8).decode(unicode_escape)

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