🎓 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
Manipulating strings is a common task in programming. Sometimes, we may need to remove characters at odd indices in a string for data processing or analysis. Python’s string manipulation capabilities allow for easy and efficient modifications of strings.
An odd indexed character in a string is a character that is located at an odd position in the string, considering the string indexing starts at 0. Therefore, the first character has an index 0 (even), the second character has index 1 (odd), and so on.
2. Program Steps
1. Define the input string.
2. Iterate over the string and collect characters that are at even indices.
3. Concatenate these characters to form the new string.
4. Display the resulting string.
3. Code Program
# Define the string
input_string = "Python Programming"
# Use string comprehension to create a new string with characters at even indices
modified_string = ''.join([input_string[i] for i in range(len(input_string)) if i % 2 == 0])
# Print the modified string
print(f"String after removing odd indexed characters: {modified_string}")
Output:
String after removing odd indexed characters: Pto rgamn
Explanation:
1. input_string is defined as "Python Programming".
2. A list comprehension is used to iterate through the string, using range(len(input_string)) to get indices of all characters.
3. The if i % 2 == 0 condition within the list comprehension checks if the index i is even.
4. If the condition is True, the character at that index is included in the new list.
5. The ''.join() function is then used to concatenate the list of characters (which now excludes characters at odd indices) into a new string modified_string.
6. The print function outputs the modified_string, which contains only the characters from the even indices of the original input_string.
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