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.


import_contacts

You may also like:

Related Posts

  • Convert dict to String in Python
    17 December

    Convert dict to string in python

    Table of ContentsHow to convert dict to string in python?Using str() method.Using the pickle module.Using the for loop.Using the json.dumps() method. In this post, we will see how to convert dict to String in Python. Python contains several data structures, and more often than not, there is a need for the conversion of one type […]

  • Split String with multiple delimiters
    17 November

    Split String With Multiple Delimiters In Python

    Table of ContentsSplitting a string in PythonWays to split a string in PythonUsing the split() and replace() functionsUsing the regular expressions In this post, we will see how to split String with multiple delimiters in Python. Splitting a string in Python When we talk about splitting a string, it means creating a collection of sub-strings […]

  • String to char Array in Python
    06 November

    Convert String to Char array in Python

    Table of ContentsConvert String To Char Array In Python Using For LoopConvert String To Char Array In Python Using The list() ConstructorConvert String To Char Array In Python Using The extend() MethodConvert String To Char Array In Python Using The += OperatorConvert String To Char Array In Python Using Packing And UnpackingConvert String To Char […]

  • Loop through String in Python
    28 October

    Loop through String in Python

    Table of ContentsUse the for loop to loop through String in PythonUsing the range() functionUsing the slicing [] operatorUsing the enumerate() functionUse the while loop to loop through String in PythonConclusion In this article, we will see how to loop through String in Python. Iteration or looping means executing a set of statements till a […]

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Python hex to bytes
    31 May

    Convert Hex to bytes in Python

    Table of ContentsUse codecs.decode() function to Convert Hexadecimal value to Byte in PythonUse bytes.fromhex() function to Convert Hexadecimal value to Byte in PythonUse the binascii Module to Convert Hexadecimal value to Byte in Python 💡 Outline You can use codecs’s decode() function to convert Hex to bytes in python. This method works for Python 2.x […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.