{"id":10210,"date":"2021-06-27T19:12:57","date_gmt":"2021-06-27T13:42:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=10210"},"modified":"2021-11-22T18:38:34","modified_gmt":"2021-11-22T13:08:34","slug":"python-program-to-calculate-the-length-of-a-string-without-using-a-library-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-the-length-of-a-string-without-using-a-library-function\/","title":{"rendered":"Python Program to Calculate the Length of a String Without Using a Library Function"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n A string is one of the most frequent data types in any computer language. A string is a collection of characters that can be used to represent usernames, blog posts, tweets, or any other text content in your code. You can make a string and assign it to a variable by doing something like this.<\/p>\n Strings are considered immutable in Python, once created, they cannot be modified. You may, however, construct new strings from existing strings using a variety of approaches. This form of programming effort is known as string manipulation.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the full approach to calculate the length of the given string without using built-in library len() in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The total number of characters in the string, which is the string’s length, is printed. Strings in Python: A string is one of the most frequent data types in any computer language. A string is a collection of characters that can be used to represent usernames, blog posts, tweets, or any other text content in your code. You can make a string and assign it to a variable by doing …<\/p>\nsample_string='BTechGeeks'<\/code><\/p>\n
given string = hello this is btechgeeks online<\/pre>\n
The length of given string { hello this is btechgeeks online } = 31<\/pre>\n
given string = for file operations upload files using 23199 -@33hello<\/pre>\n
The length of given string { for file operations upload files using 23199 -@33hello } = 54<\/pre>\n
Program to Calculate the Length of a String Without Using a Library Function in Python<\/h2>\n
\n
1)Using Count variable(Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngiven_string = 'HelloThisisBTechGeeks'\r\n# Take a variable to say stringLength that stores the length of the given string.\r\n# Initialize the stringLength to 0.\r\nstringLength = 0\r\n# To traverse the characters in the string, use a For loop.\r\nfor charact in given_string:\r\n # Increment the stringLength (Count Variable) by 1.\r\n stringLength = stringLength+1\r\n# Print the stringLength.\r\nprint('The length of given string {', given_string, '} =', stringLength)\r\n<\/pre>\n
The length of given string { HelloThisisBTechGeeks } = 21<\/pre>\n
\n
2)Using Count variable(User Input)<\/h3>\n
\n
# Give the string as user input using input() function and store it in a variable.\r\ngiven_string = input('Enter some random string = ')\r\n# Take a variable to say stringLength that stores the length of the given string.\r\n# Initialize the stringLength to 0.\r\nstringLength = 0\r\n# To traverse the characters in the string, use a For loop.\r\nfor charact in given_string:\r\n # Increment the stringLength (Count Variable) by 1.\r\n stringLength = stringLength+1\r\n# Print the stringLength.\r\nprint('The length of given string {', given_string, '} =', stringLength)\r\n<\/pre>\n
Enter some random string = btechgeeksonlineplatformforgeeks and coding platform\r\nThe length of given string { btechgeeksonlineplatformforgeeksa nd coding platform } = 52<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n