CharAt in Python

CharAt in Python

In this tutorial, we will see about CharAt in Python.

If you are already well verse in java and might be looking for charAt() alternative in Python, then you are at right place.

In Python, you can get character of String similar to the way you access array elements in java.

Here is simple code snippet to give you an simple example:

Output:

J

Similarly, you can access 4th char of String as below:

Output:

a

Here is image which will help you understand alternative of charAt in Python.
Python charAt

CharAt() in java

charAt() method in java is used to access character of String by its index. You need to pass index of the character that you want to retrieve.
Syntax

Here is simple java code snippet:

Output:

First character of String Java2blog is : J

Alternative of charAt() in Python

As you an see, java has charAt() method specifically for accessing character of String, but in Python, you can just use subscript operator to access character of String. It’s syntax is similar to the way you access elements of array in java.

Subscript operator is nothing, but square bracket([]) and you need provide index of the element that you want to access.

Output:

H

Iterate through String and print its character

Let’s now right a program to iterate through String and print its character using altearative of charAt() in Python.

Output:

J
a
v
a
2
b
l
o
g

As you can see, we have used while loop to iterate through String and subscript operator to access each character of String using its index.

That’s all about charAt in Python.

Was this post helpful?


import_contacts

You may also like:

    Related Posts

    • 19 May

      Remove First and Last Character of String in Python

      Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

    • 19 May

      Remove HTML Tags From String in Python

      Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

    • 19 May

      Remove xa0 from String in Python

      Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

    • 18 May

      Convert bool to String in Python

      Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

    • 18 May

      Convert UUID to String in Python

      Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusionWas this post helpful? In this post, we will see how to convert UUID […]

    • 18 May

      How to Replace Word in String in Python

      Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusionWas this post helpful? In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of […]

    Leave a Reply

    Your email address will not be published.

    Subscribe to our newletter

    Get quality tutorials to your inbox. Subscribe now.