0% found this document useful (0 votes)
6 views3 pages

strings_python

Uploaded by

Vishal Vikal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views3 pages

strings_python

Uploaded by

Vishal Vikal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

String

1.1 Introduc on:

Python has several data types and string among them. String represents sequence of character.
String is used for mainly for manipula ng text data in python.

There are two ways of defining the string either in single quote (‘’) or in a double quote (“ “).
Python does not have single character data type like another languages. Single character means
string of length 1.

If we look at the deeper in python the string is a class called str. You can read more on the official
documenta on on by clicking this link (h ps://docs.python.org/3/library/stdtypes.html#str).

Most of the references are taken from official documenta on of the python. You my read more
about string by clicking this link (h ps://docs.python.org/3/library/stdtypes.html#text-sequence-
type-str)

1.2 Syntax for defining a string


Variable_name = ‘string value!’
We can also use the string constructor it str(). More can be read on official documenta on
(h ps://docs.python.org/3/library/stdtypes.html#str)
There are many other ways of defining the string.
 Double quotes can be used “hello there!”
 Single quote inside a double quote “I’m python!
 If we want to use the double quote within a string we have to use the escape sequence at
the defining me of string. “ hello \”t\”here”. Some special character can also be used using
the escape sequence. More can be read on this link
(h ps://docs.python.org/3/reference/lexical_analysis.html#escape-sequences).

1.3 Indexing in string

As it is already men oned that the string is a sequence data type It can also be indexed like lists
and another data type.
a = “hello there!”
print(a[0:1]); # shows the h
#The above code has a[Star ng index:ending index:jump]

1.4 Common string opera ons

* string.ascii_le ers

The concatena on of the ascii_lowercase and ascii_uppercase constants described


below. This value is not locale-dependent.

 string.ascii_lowercase

The lowercase le ers 'abcdefghijklmnopqrstuvwxyz'. This value is not locale-


dependent and will not change.
 string.ascii_uppercase

The uppercase le ers 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. This value is not locale-


dependent and will not change.

 string.digits

The string '0123456789'.

 More can be read on the official documenta on


(h ps://docs.python.org/3/library/string.html)

1.5 String Methods

* str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range


[start, end]. Op onal arguments start and end are interpreted as in slice nota on.

If sub is empty, returns the number of empty strings between characters which is the
length of the string plus one.

 str.find(sub[, start[, end]])

Return the lowest index in the string where substring sub is found within the slice
s[start:end]. Op onal arguments start and end are interpreted as in slice nota on.
Return -1 if sub is not found.

 str.index(sub[, start[, end]])


Like find(), but raise ValueError when the substring is not found.

 There are tons of methods defined for the string in the str class you can read them on the
official python documenta on by clicking this link
(h ps://docs.python.org/3/library/stdtypes.html#string-methods)

1.6 Proper es of String

* It is an immutable data type.

* It is mainly used for the text based opera ons.

* String literals must be defined in the quotes whether single or double.

* Integer data can also be converted to the string using the str constructor of str class.

* A Plus sign can also be used for concatena ng of two strings.


1.7 Exercise:

You might also like