If you are talking about the length of the string, you can use len():

>>> s = 'please answer my question'
>>> len(s)  # number of characters in s
25

If you need the size of the string in bytes, you need sys.getsizeof():

>>> import sys
>>> sys.getsizeof(s)
58

Also, don't call your string variable str. It shadows the built-in str() function.

Answer from user225312 on Stack Overflow
Top answer
1 of 7
322

If you are talking about the length of the string, you can use len():

>>> s = 'please answer my question'
>>> len(s)  # number of characters in s
25

If you need the size of the string in bytes, you need sys.getsizeof():

>>> import sys
>>> sys.getsizeof(s)
58

Also, don't call your string variable str. It shadows the built-in str() function.

2 of 7
49

Python 3:

user225312's answer is correct:

A. To count number of characters in str object, you can use len() function:

>>> print(len('please anwser my question'))
25

B. To get memory size in bytes allocated to store str object, you can use sys.getsizeof() function

>>> from sys import getsizeof
>>> print(getsizeof('please anwser my question'))
50

Python 2:

It gets complicated for Python 2.

A. The len() function in Python 2 returns count of bytes allocated to store encoded characters in a str object.

Sometimes it will be equal to character count:

>>> print(len('abc'))
3

But sometimes, it won't:

>>> print(len('йцы'))  # String contains Cyrillic symbols
6

That's because str can use variable-length encoding internally. So, to count characters in str you should know which encoding your str object is using. Then you can convert it to unicode object and get character count:

>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols 
3

B. The sys.getsizeof() function does the same thing as in Python 3 - it returns count of bytes allocated to store the whole string object

>>> print(getsizeof('йцы'))
27
>>> print(getsizeof('йцы'.decode('utf8')))
32
🌐
W3Schools
w3schools.com › python › gloss_python_string_length.asp
Python String Length
The len() function returns the length of a string: a = "Hello, World!" print(len(a)) Try it Yourself » · Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative ...
Discussions

I’m scared of learning a new language
If you just "learn" a programming language without actually applying it in proper projects, you won't retain any knowledge. Also, why are you jumping from one language to another so frequently? That's one of the biggest mistakes beginners make. Stick with one language (by one language, I mean a set of languages for a particular tech stack) and be really good with it before you jump to another one. If you're forced to learn new languages due to academic reasons, you can learn them to do your coursework, but stick with one language as your goto/main language for your personal projects and whatnots. It's more important to learn design patterns, software architecture and system design principles than learn the syntax of another language. It doesn't matter which C-like language you use to do that. So stop forcing yourself to learn new languages and focus mastering the fundamentals of software development first. It'll be extremely easy to pick up new languages once you've done that. TLDR: Stop laguage hopping. More on reddit.com
🌐 r/learnprogramming
19
2
July 11, 2025
How to count number of sentences in a python messy string
is there a \n after every sentence? if so, just print(len(yourstring.split("\n"))) if not, I've created a quick and dirty example of what you could do. somestring = your messy string here, imported from your text file q_mark = somestring.split("?") ex_mark = [a.split("!") for a in q_mark] temp_list = [] for i in ex_mark: temp_list = temp_list + i temp_list_2 = [] dot = [a.split(".") for a in temp_list] for i in dot: temp_list_2 = temp_list_2 + i print(len(temp_list_2)) It's not elegant, but it works. kinda. abrevations typically end with a dot, so you'll end up with more "sentence counts" than you really have if your string has lots of abreviations. More on reddit.com
🌐 r/learnpython
9
4
January 3, 2018
Pad out all strings in a list the length of the longest string
string.ljust(len(maxlen)) This returns a new string, but your code doesn't save it anywhere. You should make a new list containing the padded strings. This is exactly what list comprehensions are for: padded_list = [s.ljust(maxlen) for s in list] By the way, maxlen = len(max(list, key=len)) or maxlen = max(len(s) for s in list) More on reddit.com
🌐 r/learnpython
3
3
September 12, 2013
How to find max length of a string in a list AND the column index

I have some not very clean code for it

  1. You need to know terminal size

     def getTerminalSize():
    env = os.environ
    def ioctl_GWINSZ(fd):
    try:
    import fcntl, termios, struct
    cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
    '1234'))
    except:
    return
    return cr
    cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
    if not cr:
    try:
    fd = os.open(os.ctermid(), os.O_RDONLY)
    cr = ioctl_GWINSZ(fd)
    os.close(fd)
    except:
    pass
    if not cr:
    cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
    return int(cr[1]), int(cr[0])
More on reddit.com
🌐 r/learnpython
2
3
August 5, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-length-of-a-string-in-python-4-ways
Find Length of String in Python - GeeksforGeeks
October 27, 2025 - Explanation: len(a) directly returns the number of elements in the object a. In this method, we manually count characters by iterating through the string using a for loop. Each iteration increases the counter by one, giving the total string length.
🌐
Mimo
mimo.org › tutorials › python › how-to-find-the-length-of-a-string-in-python
How to Find the Length of a String in Python
Use len(text) to get the number of characters in a string. An empty string has length 0. Spaces, tabs, and newlines count as characters unless you strip them. Use .strip() when whitespace-only input should count as empty. Decide how to handle None before calling len(). ... Become a Python developer.
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
Like many other popular programming languages, strings in Python are arrays of unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1.
🌐
Educative
educative.io › answers › how-to-find-the-length-of-a-string-in-python
How to find the length of a string in Python
To calculate the length of a string in Python, you can use the built-in len() method.
🌐
StrataScratch
stratascratch.com › blog › how-to-determine-python-string-length
How to Determine Python String Length? - StrataScratch
August 1, 2024 - Let’s say you want to measure the length of the strings in Python, but how? Meet len(), your new best friend, for string measurements.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-length-len
Python string length - GeeksforGeeks
October 15, 2024 - It returns an integer which is the length of the string. The len() method is used to find the length of an object in Python. By using len(), we can find the length of strings and lists.
🌐
Codefinity
codefinity.com › courses › v2 › 04fb3c65-4043-4ee3-a995-cba5bdd807af › b427d60d-9690-43e9-8110-556cc8891a2c › d6dde138-847e-4418-aa6f-8155eae8115e
Learn Challenge: String Length | Strings
The len() function counts every character in the string, including spaces. In the example, "Practice is the key to success" has 30 characters, so len(text) returns 30. ... Thanks for your feedback!
🌐
AlgoCademy
algocademy.com › link
String Length in Python | AlgoCademy
The len() function in Python returns the number of characters in a string. This includes letters, numbers, spaces, and special characters. Knowing the length of a string is crucial for various operations, such as slicing, indexing, and looping through the string.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get the Length of a String (number of Characters) in Python | note.nkmk.me
April 30, 2025 - In Python, you can get the length of a string (str), which is the number of characters, using the built-in len() function. Built-in Functions - len() — Python 3.13.3 documentation Get the length of a ...
🌐
Tutorialspoint
tutorialspoint.com › python › string_len.htm
Python String len() Method
The Python String len() method is used to retrieve the length of the string. A string is a collection of characters and the length of a string is the number of characters (Unicode values) in it.
🌐
Metaschool
metaschool.so › home › answers › how to use python string length?
How to Use Python String Length?
December 6, 2024 - To find out string length in Python, you can use the len() function. This built-in function returns the number of characters in the string, including letters, numbers, spaces, and punctuation.
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to find the length of a string in python
Find Length of a String in Python - 5 Easy Methods
December 18, 2025 - Jumping right into ways you can find a string’s length in Python, here are the most used methods you can use for the purpose: Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills ... The len() function is ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › string length python
String Length Python | How to Find Length of String in Python?
October 9, 2023 - We’ll also delve into examples ... and escape effectively. ... In Python, you can find the length of a string using the len() function....
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
LinkedIn
linkedin.com › all › engineering › programming
How do you determine the length of a string in Python?
January 16, 2024 - Determination. Delivery. 🏆 LinkedIn Top Data Analytics Voice ... The simplest and most common way to determine the length of a string in Python is to use the built-in len() function.
🌐
Altcademy
altcademy.com › blog › how-to-get-the-length-of-a-string-in-python
How to get the length of a string in Python - Altcademy.com
June 13, 2023 - Now that we've understood the intuition behind the length of a string, let's see how to find it in Python. Python provides a built-in function called len() that returns the length of a given string.
🌐
Cisco
ipcisco.com › home › python string length
Python String Length | How to Calculate String Lenght in Python?⋆ IpCisco
December 27, 2021 - With Python Strings, to find the length of a string, one of the most used method is len () function. With len () function, we can measure the length of the string. In other words, python len () function returns with python string length.
🌐
Unstop
unstop.com › home › blog › python string length | 10 ways explained with code examples
Python String Length | 10 Ways Explained With Code Examples
January 14, 2025 - The length of a string is simply the number of characters it contains, including spaces, punctuation, and special characters. Knowing the length of a string in Python programming is fundamental for many reasons, like:
🌐
WsCube Tech
wscubetech.com › resources › python › programs › length-of-string
How to Find Length of a String in Python? (5 Programs)
2 weeks ago - Learn how to find the length of a string in Python with 5 different programs. Explore multiple methods with examples, outputs, and explanations. Read now!