String
A string in Python consists of a series or sequence of characters - letters, numbers, and special characters. Strings can be indexed - often synonymously called subscripted as well. Similar to C, the first character of a string has the index 0.
Python use utf-8 encoding to define charcters
String can be defined in single quotes as 'any string' or in double quotes as "any string"
In python String can also be defined as
""" line1
line2
multiline stirng """
or
''' para1
para2
para3
multiline'''
String data type is a object of built-in String Class
Data Structure in Python
Finding ASCII values in python
97
'd'
Python is different in handling strings
Generally in other languages like c++, java, it reads string as array and characters
In Python, name = "a" is treated as string of length=1
Python deals only with strings, it doesn't know anything about characters
String is immutable data structure in python
How is it Immutable?
a = "Sahil", a[0] = "p" is not allowed in python
String Operation:
Case-1:
10-20-30
Case-2:
10-20-30
Case-3:
This will give error
Case-4:
sahil-20-30
Case -5:
20-30-sahil
Example - 1:
hello world
Hello World
Hi this is Strings in python.
Python is an awesome language.
Strings are wide data type.
We can convert almost all data types into strings.
Default input function of python returns a string.
Example - 2:
He said,"She is beautiful."
He said,"She is beautiful."
Properties of string
Indexing # starts from 0 to n - 1 where n is the length of string use []
Slicing # used to make sub-strings from strings used with start,end and step variable
Example - 3:
h
h
Example - 4:
lo World
Hello World!
Hello
World!
Hello World!
Example - 5:
!dlroW olleH
Hello World
Hello
!dlroW olleH
HloWrd
!lo le
In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.
Example - 6:
Gyansetu and Attributes
hello world
hello
world
Some Basic Functions of String
swapcase
upper
lower
strip, lstrip, rstrip
find
replace
format
center
Example - 7:
Enter a String : ALL Dogs ARE Not cats
ALL Dogs ARE Not cats
Swapcase : all dOGS are nOT CATS
Upper : ALL DOGS ARE NOT CATS
Lower : all dogs are not cats
Strip : ALL Dogs ARE Not cats
LStrip : ALL Dogs ARE Not cats
RStrip : ALL Dogs ARE Not cats
Enter pattern to search : cats
Find Result : 21
String : ALL Dogs ARE Not cats Pattern : cats
* ALL Dogs ARE Not cats**
replace() is an inbuilt function in Python programming language that returns a copy of the string where all occurrences of a substring is replaced with another substring.
string.replace(old, new, count) #syntax
old – old substring you want to replace.
new – new substring which would replace the old substring.
count – the number of times you want to replace the old substring with the new substring. (Optional)
Example - 8:
HI hello HI hello HI hello HI hello
HI HELLO HI HELLO hi HELLO hi hello
Last updated