Python Tutorial : Get the length of the string in Python
Hi folks,
As I have started learning Python, I thought why not to share the knowledge with you too?
So I first encountered the problem when I started working with strings.
I was trying to find a way to get the length of a string I am passing into a function,
So I have written a function as:
As I have started learning Python, I thought why not to share the knowledge with you too?
So I first encountered the problem when I started working with strings.
I was trying to find a way to get the length of a string I am passing into a function,
So I have written a function as:
def getLengthOfString(value):
stringValue = str(value) stringLength = len(value) print(stringLength)
getLengthOfString("Hello, Please print the length of this string")
And here is the output which comes in my screen :
Explanation :
1.In first line we define a function with the name getLengthofString, which accepts a parameter.
2. In second line, As python can accept anything as a parameter I am converting the passed parameter into an string by using an inbuilt function str and storing into another variable.
3. Now I am getting the length of the parameter by again using an inbuilt function len and passing the string value into this.
4. In last I am printing the length on the screen, you can see the result 45 highlighted in the screen shot.
Note: I am using PyCharm IDE to demonstrate this tutorial.
Leave a Comment