Python3 Strings DS
Python Data Structures : Strings
Strings :
A string is a sequence of characters.
***Computer do not deal with characters, they deal with numbers (binary) .Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0's and 1's.
This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and Unicode are some of the popular encoding used.
* In python, string is a sequence of Unicode character.
How to create a string?
-> Strings can be created by enclosing characters inside a single quote or double quotes.
-> Even triple quotes can be used in python but generally used to represent multiline strings and docstrings.
Eg:
mystring='Hello'print(mystring)mystring="Hello"print(mystring)mystring=' ' 'Hello' ' 'print(mystring)
output: Hello
Hello
Hello
How to access characters in a string?
Eg:
mystring="Hello"#print first characterprint(mystring[0])print(mystring[-1])print(mystring[2:5])
output: H
o
llo
If we try to index out of range or use decimal number, we will get type error.
How to change or delete a string ?
Strings are immutable. This means that elements of a string cannot be changed once it has been assigned.
We can simply reassign different strings to the same name.
We cannot delete or remove character from a string. But deleting the string entirely is possible using the keyword del.
Eg:
del mystringprint(mystring)
output: ----------------------------------------------------------------
--
NameError
NameError: name 'mystring' is not defined(because it is deleted)
String Operations :
Concatenation :
Joining of two or more strings into a single one is called concatenation.
The + operation does this in python. Simply writing two string literals together also concatenates them.
The * operator can be used to repeat the string for a given number of times.
Eg:
s1="Hello"s2="Sujith"print(s1+s2) #concatenate 2 stringsprint(s1*3) #repeat string n times
output: Hello Sujith
Hello Hello Hello
Iterating Through String :
Eg:
count=0for l in "Hello World":if l=='o':count+=1print(count, 'letters found')
output: 2 letters found
String Membership Test :
print('l' in 'Hello world')
output: True
print('or' in 'Hello world')
output: True
String Methods :
Some of the commonly used methods are lower( ), upper( ), join( ), split( ), find( ), replace( ) etc.
Eg:
"Hello".lower( )
output: 'hello'
"Hello".upper( )
output: 'HELLO'
"This will split all words in a list".split( )
output: ['This','will','split','all','words','in','a','list']
' '.join(['This', 'will', 'split', 'all', 'words', 'in', 'a', 'list'])
output: 'This will split all words in a list'
"Good Morning".find("Mo")
output: 5
"Bad Morning".replace("Bad","good")
output: 'Good Morning'
Python Program to Check where a String is Palindrome or not ?
mystr="MadaM"myStr=mystr.lower( )revstr=reversed(myStr)if list(myStr)==list(revstr):print("Given string is palindrome")else:print("Given string is not palindrome")
output: Given string is palindrome
Python Program to Sort Words in Alphabetic Order ?
mystr="python program to sort words in alphabetic order"words=mystr.split( )words.sort( )for word in words:print(word)
output: alphabetic
order
program
sort
in
python
to
words
Comments
Post a Comment