In python strings are character enclosed in quotes of any type i.e. Single quotation marks or double quotation marks.
Note:- Strings are immutable i.e. any character inside the string cannot be changed once declared i.e. str[i]='h' is invalid
- Square brackets can be used to access elements of the string. i.e. str[i]
- The String are sequence of character, where each character has a unique poistion id/index. The indexs of a string beigns from 0 to (length-1) from left to right and from -1 to -(length) from right to left
Example: "hello", 'hello', "436", "45.69", "[4,5,6]", " ", "today is 1/11/2020"
s="Computer"
for i in s:
print(i)
s='computer'
for i in range(0,len(s)):
print(s[i])
s1="Computer"
s2="Science"
final=s1+" "+s2
print(final)
s1="Hello"
final=s1*3
print(final)
'ter' in 'Computer'
'ter' not in 'Computer'
Comparison operator:
All realational operator (<,>,<=,>=,!=) can be used with string in python
print('a'=='a')
print('a'>'abc')
print('a'!='a')
print('Abc'>'abc') #in ASCII A=65 and a=97
print('Abc'<='Abc')
#Note to find ASCII value of a character we can use ord function
print(ord('A'))
print(ord('a'))
sub="Computer Science"
print(sub[2:8])
print(sub[2:8:2])
string.capitalize(): Converts the first letter of the first word of a string into uppercase
sub="computer"
print(sub.capitalize())
string.lower(): Converts the string to lower case
sub="COMPUTER"
print(sub.lower())
string.upper(): Converts the string into upper case
sub="computer"
print(sub.upper())
len(string): Returns the number of character in the string
sub="computer"
len(sub)
string.title(): it converts first letter of each word in the string into uppercase
sub="computer science"
print(sub.title())
string.count(sub_string): String count() function returns the number of occurrences of a substring in the given string.
string = "Python is awesome, isn't it?"
substring = "is"
val = string.count(substring)
print(val)
string.find(value): The find() method finds the position of first occurrence of the specified value in a string. Return -1 if not found
sub="Computer Science"
value="ter"
pos=sub.find(value)
print(pos)
string.index(value): The index() method finds the first occurrence of the specified value. The index() method is almost the same as the find() method, the only difference is that the find() method returns -1 if the value is not found and index() function through exception.
sub="Computer Science"
value="ter"
pos=sub.index(value)
print(pos)
string.split("separator"): The split() function splits a string into a list based on the seperator character.
txt="today,is,a,good,day"
ls=txt.split(",")
print(ls)
string.partition("sub_string"): The partition() method searches for a specified string, and splits the string into a tuple containing three elements.
txt="today is good Friday"
ls=txt.partition("is")
print(ls)
string.strip("character"): removes spaces(by default) or character from the beginning and end of the string.
txt=" Computer "
print(txt.strip())
txt=".Computer."
print(txt.strip('.'))
string.lstrip(): removes spaces(by default) or character from the beginning of the string.
txt=" Computer "
print(txt.lstrip())
txt=".Computer."
print(txt.lstrip('.'))
string.rstrip(): removes spaces(by default) or character from the end of the string.
txt=" Computer "
print(txt.rstrip())
txt=".Computer."
print(txt.rstrip('.'))
string.replace(oldvalue, newvalue, count) The replace() method replaces a specified phrase with another specified phrase.
txt="today is good Friday"
ls=txt.replace("good","very good")
print(ls)
string.isalnum(): Return True if the character in the string are alphanumeric(alphabet or number) and there is atleast one character in the string else return False
str1="abc12"
print(str1.isalnum())
str2=""
print(str2.isalnum())
string.isalpha(): Return True if the character in the string are alphabet and there is atleast one character in the string else return False
str1="abc"
print(str1.isalpha())
str2="abc123"
print(str2.isalpha())
string.isdigit(): Return True if the character in the string are number and there is atleast one character in the string else return False
str1="12345"
print(str1.isdigit())
str2="abc"
print(str2.isdigit())
string.islower(): Return True if all the character in the string are in lower case and there is atleast one character in the string else return False
str1="Abc"
print(str1.islower())
str2="abc"
print(str2.islower())
string.isupper(): Return True if all the character in the string are in upper case and there is atleast one character in the string else return False
str1="ABC"
print(str1.isupper())
str2="Abc"
print(str2.isupper())
string.isspace(): Return True if there are all whitespace character in the string and there is atleast one character in the string else return False
str1=" "
print(str1.isspace())
str2=""
print(str2.isupper())
Practice Program:
- Write a python program to print a string in reverse order character by character in a new line
- Write a program that reads a line and prints its statistics like:
a. Number of uppercase letter
b. Number of lowercase letter,
c. Number of alphabet
d.Number of digit
Sample String: "3.14 is the value of PI"