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

วิธีหาขนาดของ String ใน Python

1. ความยาว (จำนวนตัวอักษร): ใช้ len()

>> len('Hello World!')
12

2. ขนาดในหน่วย Bytes: ใช้ sys.getsizeof()

>> from sys import getsizeof
>>> getsizeof('Hello World!')
33

getsizeof() รวม overhead ของ object ด้วย