Display first character of a string

String is a collection of characters. Python has many inbuilt function to work with string. Each character in a string has index number. Index number always start with 0. For example string ‘Python’ has 6 characters and below is the index position of each character in string.

Character          P  y  t  h  o  n
Index number  0  1  2  3  4  5

In this blog we will ask user to enter any string, display the string entered by user and then display first character of a string.

We will use index position to display character inside string.

Asking user to enter any string.

str = input('Please enter any string: ')

Display the string entered by user.

print('You entered -> %s'%str)

To display first character of a string use index number 0. Index number should be enclosed within square brackets.

print('First Character of a string : %s'%str[0])

OUTPUT

Please enter any string: Batman
You entered -> Batman
First Character of a string : B

Complete Code

str = input('Please enter any string: ')
print('You entered -> %s'%str)
print('First Character of a string : %s'%str[0])

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s