
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to get the size of a string in Python?
Python has a method called len() that gives us the length of any composite object. To get the length of a string, just pass the string to the len() call.
example
print(len('Hello World!'))
Output
12
If you want the size of the string in bytes, you can use the getsizeof() method from the sys module.
example
from sys import getsizeof getsizeof('Hello World!')
Output
33
You can read more about it at sys.getsizeof()
- Related Articles
- How to get the size of a list in Python?
- Python program to get all permutations of size r of a string
- How to get the length of a string in Python?
- How do we get the size of a list in Python?
- How do we get size of a list in Python?
- How to get the Tab Size of a JTextArea in Java?
- How to get the size of a json object in JavaScript?
- How to get first 100 characters of the string in Python?
- Split String of Size N in Python
- How to get the size of the tables of a MySQL database?
- How to get integer values from a string in Python?
- How to get a space-padded string with the original string right-justified in Python?
- How to get a variable name as a string in Python?
- How to get a function name as a string in Python?
- How to get the screen size in Tkinter?

Advertisements