🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
While Python provides a built-in len() function to determine the length of a string, understanding how to manually compute this can be an insightful exercise. In this post, we'll explore a simple way to calculate the length of a string in Python without using any built-in functions.
2. Program Overview
1. Create a function to iterate through the string and count the number of characters.
2. Take user input for the string.
3. Call the function with the user-provided string.
4. Display the result.
3. Code Program
# Python program to calculate the length of a string without using built-in functions
def string_length(s):
"""Function to calculate the length of a string."""
count = 0 # Initialize count to 0
for char in s:
count += 1 # Increment count for every character
return count
# Taking user input
string_input = input("Enter a string: ")
# Calculate the length of the string
length = string_length(string_input)
# Display the result
print(f"The length of the string '{string_input}' is: {length}.")
Output:
(For an input of "Python") Enter a string: Python The length of the string 'Python' is: 6.
4. Step By Step Explanation
1. We start by defining a function named string_length that takes a single string argument s.
2. Inside the function, we initialize a variable count to zero. This variable will store the length of the string.
3. We then iterate over each character in the string using a for loop. For every character, we increment the count by 1.
4. After processing all characters, the function returns the value of count which represents the length of the string.
5. Outside the function, we prompt the user to provide a string.
6. We then call the string_length function with the user-input string and store the result in the length variable.
7. Finally, we display the length of the string using a formatted print statement.
8. The provided output section illustrates the program's output for the input string "Python".
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment