0% found this document useful (0 votes)
53 views6 pages

Strings: What Is String in Python?

This document discusses strings in Python. It defines a string as a collection of characters that is written within single quotes, double quotes, or triple quotes. It describes how to create strings using different quote types and how to access individual characters or slices of a string using indexes and slicing notation. It also covers deleting strings, updating string values, and formatting strings using the format() method.

Uploaded by

GAURAV GOSWAMI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views6 pages

Strings: What Is String in Python?

This document discusses strings in Python. It defines a string as a collection of characters that is written within single quotes, double quotes, or triple quotes. It describes how to create strings using different quote types and how to access individual characters or slices of a string using indexes and slicing notation. It also covers deleting strings, updating string values, and formatting strings using the format() method.

Uploaded by

GAURAV GOSWAMI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

STRINGS

What Is String in Python?


Many of us who are familiar with programming languages like C, C++ etc. will have an
answer like “String is a collection or array of characters.”

Well in Python also, we say the same definition for String data type. String is array of
sequenced characters and is written inside single quotes, double quotes or triple quotes.
Also, Python doesn’t have character data type, so when we write ‘a’, it is taken as a string
with length

How to create a string?

Input:
s = "hello"

print(s)

s1 = "hello"

print(s1)

s2 = ''' Hello

how is the whether today '''

print(s2)

Output:
hello

hello

Hello

how is the whether today

>>>

Triple quotes are used usually when we have both single and double quotes in the string
and also when we want to write multi-line sentences.
How to access a character from a string?

Suppose we want to access a character from a string, let’s say the last character, we need to
know it’ s position in the string.

Here is a string along with the position allocated. So if we want to access ‘n’ from the string, we
have to go to the 5th position to have it.

The numbering or indexing starts from 0 to one less than the length of string.

Here is a python program to make us more clear in that.

Input:

str = "antartica is really cold."

print('str= ', str)

# first character

print('str[0] = ', str[0])

#last character
print('str[-1] = ', str[-1])

#slicing 2nd to 5th character

print('str[1:5] = ', str[1:5])

#slicing 6th to 2nd last character

print('str[5:-2] = ', str[5:-2])

Output:
str= antartica is really cold.
str[0] = a
str[-1] = .
str[1:5] = ntar
str[5:-2] = tica is really col
>>>
>
Now if left to right follows increasing order pattern in indexing, right to left follows
decreasing order pattern i.e. from -1, -2, -3 so on. So if you want to access the last
character, you can do it in two ways.

Input:
str = 'Antarctica is really cold.'
a = len(str)
print('length of str ', a)
#last character with the help of length of the string
print('str[a] ', str[a-1])
#last character with the help of indexing
print('str[-1] ',str[-1])

Output:
length of str 26
str[a] .
str[-1] .
>>>
However you can delete the whole string with del operator.

Input:

s = "Hello Batman"
print(s)
del s
print(s)

Output:
Hello Batman
Traceback (most recent call last):
File "C:/Users/sandeep/Desktop/strings.py", line 4, in <module>
print(s)
NameError: name 's' is not defined

>>>
>If you don’t want s to be “Hello Batman” and want it to be some other string, then
you can just update the string as a whole.

Input:
s = "Hello Batman"
print(s)
s = "Hello Spiderman"
print(s)

Output:
Hello Batman
Hello Spiderman

>>>
Moving on with this article on what is String in Python?

Formatting a String:
Formatting a string means to allocate the string dynamically wherever you want.

Strings in Python can be formatted with the use of format() method which is very versatile and
powerful tool for formatting of Strings. Format method in String contains curly braces {} as
placeholders which can hold arguments according to position or keyword to specify the order.

Input:
String1 = "{} {} {}".format('Hello', 'to', 'Batman')
print("Default order: ")
print(String1)
# Positional Formatting
String1 = "{1} {0} {2}".format('Hello', 'to', 'Batman')
print("nPositional order: ")
print(String1)
# Keyword Formatting
String1 = "{c} {b} {a}".format(a='Hello', b='to', c='Spiderman')
print("nString in order of Keywords: ")
print(String1)
# Formatting of Integers
String1 = "{0:b}".format(20)
print("binary representation of 20 is ")
print(String1)
# Formatting of Floats
String1 = "{0:e}".format(188.996)
print("nExponent representation of 188.996 is ")
print(String1)
# Rounding off Integers
String1 = "{0:.2f}".format(1 / 6)
print("none-sixth is : ")
print(String1)
# String alignment
String1 = "|{:<10}|{:^10}|{:>10}|".format('Hello', 'to', 'Tyra')
print("nLeft, centre and right alignment with Formatting: ")
print(String1)

Output:
Default order:
Hello to Batman
nPositional order:
to Hello Batman
nString in order of Keywords:
Spiderman to Hello
binary representation of 20 is
10100
nExponent representation of 188.996 is
1.889960e+02
none-sixth is :
0.17
nLeft, centre and right alignment with Formatting:
|Hello | to | Tyra|
>>>

You might also like