Python program to get a character from a given string

WBOY
Release: 2023-08-26 15:53:06
forward
2259 people have browsed it

Python program to get a character from a given string

In Python, we can get a character from a given string using the indexing operator '[]', using slicing and using indexes separated by colons. We can easily get characters from a string by passing the index of the character we want to access to the index operator. In this article, we will see how to access the characters of a string using indexing operators.

Use [ ] operator

grammar

string[index]
Copy after login

The string here is the given string in which we want to access specific characters. The index is the index of the character in the string.

Example 1

In the following example, we initialize a string "Hello World" and use the index property to get the character at position 0.

string = "Hello World"
print(string[0])
Copy after login

Output

H
Copy after login

Example 2

You can access any character in a string using its index. In the example below, we use index 2 to get the third character of the string.

string = "Hello World"
print(string[-1])
Copy after login

Output

d
Copy after login
Copy after login

Example 3

The last index of a string can also be accessed using a negative index. In the example below, we create a string "Hello World". We can access the last character of the string by passing the index as -1 to the index operator ([ ]).

string = "Hello World"
print(string[10])
Copy after login

Output

d
Copy after login
Copy after login

Use slices

Slicing is used to get multiple characters from a string/Slicing is similar to range, but more precise. The slice method takes multiple characters from a string using a start index, an end index, and a step size (i.e. "start:end:step"). The step size represents the number of jumps to get characters from the string.

Example

To get every space character in the string, we can use a stride of 2. To instruct the slicing method to scan from the first character of the string to the last character, we can leave the start index and end index blank.

my_string = "Hello, World!"
every_other_character = my_string[::2]
print(every_other_character)
Copy after login

Output

Hlo ol!
Copy after login

Use colon-separated indexes

Example 1

We can access multiple characters in a string by using a series of indices. We provide a starting index and an ending index separated by colons. The starting index character is contained in multiple characters, but the ending character is not contained in multiple strings we are trying to access. We can access the first three characters of the string "Hello, World!" as follows -

my_string = "Hello, World!"
first_three_characters = my_string[0:3]
print(first_three_characters)
Copy after login

Output

Hel
Copy after login

Example 2

To get characters with indexes from 6 to 11, we use the range 6:12.

my_string = "Hello, World!"
characters_6_to_11 = my_string[7:12]
print(characters_6_to_11)  # Output: World
Copy after login

Output

World
Copy after login

in conclusion

In this article, we discussed how to access arbitrary characters of a string in a simple way using the indexing operator ([ ]). Index operators are used in almost all programming languages.

The above is the detailed content of Python program to get a character from a given string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template