Register for 45 Day Coding Challenge by CodeStudio And Win Some Exciting Prizes

# Python: Get the First Character of String

Here, we will develop a Python program to get the first character of a string. If the string was “Knowprogram” then print the first character “K”. We will discuss how to get the first character from the given string using native methods, and slice operator. Also, we will develop a Python program to get the first two characters of a string.

## Get First Character of String in Python

We will take a string while declaring the variables. Then, we will run the loop from 0 to 1 and append the string into the empty string (first_char). Finally, the first character will be displayed on the screen.

``````# Python Program get first character of string

# take input
string = input('Enter any string: ')

# get first character
first_char = ""
for i in range(0, 1):
first_char = first_char + string[i]

# printing first character of string
print('First character:', first_char)``````

Output for the different input values:-

Enter any string: Python
First character: P

Enter any string: Know Program
First character: K

## Python Program for First Character of String

In python, String provides an [] operator to access any character in the string by index position. We need to pass the index position in the square brackets, and it will return the character at that index. As indexing of characters in a string starts from 0, So to get the first character of the given string pass the index position 0 in the [] operator i.e.

``````# Python Program get first character of string

# take input
string = input('Enter any string: ')

# get first character
first_char = string[0]

# printing first character of string
print('First character:', first_char)``````

Output:-

Enter any string: character
First character: c

## Get First Character of String using Slicing

We will get the first character of the string using the slice operator. The [:1] specifies the character at index 0. The string[:1] specifies the first characters of the given string.

``````# Python Program get first character of string

# take input
string = input('Enter any string: ')

# get first character
first_char = string[:1]

# printing first character of string
print('First character:', first_char)``````

Output:-

Enter any string: first
First character: f

## Python program to Get First Two Character of String

In the previous program, we will discuss how to get the first character of the string but in this program, we will discuss how to get the first two characters of the given string.

``````# Python Program get first two character of string

# take input
string = input('Enter any string: ')

# get first two character
first_two = string[:2]

# printing first two character of string
print('First character:', first_two)``````

Output:-

Enter any string: Two character
First character: Tw

## Python Program for First Letter of String

This python program is different from the above program, in this program, we will print all first characters of the given string. If the string was “Know Program” then print all first characters “KP”.

``````# Python Program get first character from a string

# take input
string = input('Enter any string: ')

# get first character
first_char = ''.join([s[:1] for s in string.split(' ')])

# printing first character of string
print('First character:', first_char)``````

Output for the different input values:-

Enter any string: Know Program
First character: KP

Enter any string: First character of a string
First character: Fcoas

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!