DEV Community

Divya Divya
Divya Divya

Posted on

String in Python

In Python, a String is one of the most commonly used data types. It is used to store and manipulate text such as names, messages, and sentences.

What is a String?

A String is a sequence of characters enclosed within quotes.

Example:

name = "Divya"
message = 'Hello World'

Here:

"Divya" and 'Hello World' are strings
Characters include letters, numbers, and symbols

Methods :

The simplest way to get the first character is by using index 0.
The simplest way of get the Last character index = length - 1.

Example

name="divya"
print(name[0])                                  //d
print(name[len(name)-1])                       //a  
print(name[len(name)-1].upper())              //D
print(name.upper())                          //DIVYA

Enter fullscreen mode Exit fullscreen mode

String Access in Loop

name="divya"
i=0
while i<len(name):
    print(name[i])
    i+=1                                     //d
                                               i
                                               v
                                               y
                                               a

Enter fullscreen mode Exit fullscreen mode

Top comments (0)