• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / for char in string – How to Loop Over Characters of String in Python

for char in string – How to Loop Over Characters of String in Python

August 22, 2022 Leave a Comment

To loop over the characters of a string in Python, you can use a for loop and loop over each character in the following way.

string = "example"

for char in string:
    print(char)

#Output:
e
x
a
m
p
l
e

When working with strings, the ability to work with the characters individually is valuable.

You can loop over the characters of a string variable in Python easily.

String objects are iterable in Python and therefore you can loop over a string.

To loop over the characters of a string in Python, you can use a for loop and loop over each character in the following way.

string = "example"

for char in string:
    print(char)

#Output:
e
x
a
m
p
l
e

Examples of Using for char in string in Python to Loop Over String

Depending on the situation, you might find it useful to want to loop over the characters of a string.

One example of using ‘for char in string’ is if you want to check if a string contains only certain characters.

In Python, we can easily get if a string contains certain characters in a string looping over each character of the string and seeing if it is one of the given characters or not.

Below is a function which will check if a string has certain characters or not for you in a string using Python.

def containsCertainChars(string, chars):
    for char in string:
        if char in chars:
           return True
    return False

print(containsCertainChars("Hello World!", "H"))
print(containsCertainChars("Hello World!", "olz"))
print(containsCertainChars("Hello World!", "z"))

#Output:
True
True
False

Another example of using ‘for char in string’ to loop a string is to check if a string contains uppercase letters in Python.

To check if a string contains uppercase, we just need to loop over all letters in the string until we find a letter that is equal to that letter after applying the upper() function.

Below is a Python function which will check if a string contains uppercase characters.

def checkStrContainsUpper(string):
    for x in string:
        if x == x.upper():
            return True
    return False

print(checkStrContainsUpper("all letters here are lowercase"))
print(checkStrContainsUpper("We Have some uppercase Letters in this One."))

#Output:
False
True

Hopefully this article has been useful for you to learn how to loop over a string with ‘for char in string’ in Python.

Other Articles You'll Also Like:

  • 1.  Check if a Number is Divisible by 2 in Python
  • 2.  pandas startswith() – Check if String Starts With Certain Characters
  • 3.  Using Python to Count Number of Lines in String
  • 4.  Using Python to Count Items in List Matching Criteria
  • 5.  Using Selenium to Get Text from Element in Python
  • 6.  Using Python to Check if String Contains Only Letters
  • 7.  Remove Time from Datetime in Python
  • 8.  Concatenate Multiple Files Together in Python
  • 9.  Truncate String in Python with Slicing
  • 10.  Squaring in Python – Square a Number Using Python math.pow() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy