regularpython@gmail.com
Strings
A group of characters is called string. The string is immutable means we cannot modify once we declared it. Strings can be represented by a single quotation (‘python’) or double quotation (”python”) or triple quotation (’’’python’’’).
Examples:
name = ‘python’
name = “python”
name = “’ python ’’’
When we use single, double or triple quotations?
Single Quotation:
if you want to print a sentence like this (Python is a very good “programming language” for beginners.). We can’t use double quotation.
Example:
Sentence = “Python is a very good “programming language” for beginners.”
Sentence = ‘Python is a very good “programming language” for beginners.’
Double quotation:
If you want to write a sentence like this (Mother’s Day) you can’t write this sentence inside the single quotation. If you write it, you’ll get syntax errors.
Example:
Sentence = ‘Mother’s Day’ This statement is wrong.
Sentence = “Mother’s Day” This statement is right.
Triple Quotation:
If we want to take more lines then we use a triple quotation.
Example:
’’’
bshbfsb f usfbsiuf sufiubsifb biubsduf bdbfb
Dn dviuduv duibvudbbdbbdvb dbvui
Vjdvndbv dvduvuidvdn dnubdunbuidb
‘’’
String Special Operators
Ø + (Concatenation)
Ø *(Repetition)
Ø [] (Slice)
Ø [:] (Range Slice)
Ø In (Membership operator)
Ø % (String Format)
+ (Concatenation): This operator is used to add strings.
Example:
name = "regularpython"+ " is a website name"
output: regularpython is a website name
add = "5"+ "5"
print(add)
output:55
* (Repetition operator) : This operator is used to repeat the string.
Example:
name = "python "*5
print(name)
[](slice): This is used to slice the string.
Example:
name = "python"
a = name[2:]
print(a)
output:
thon
[:] (Range Slice): This is also used to slice the string.
Example:
name = "python"
a = name[2:5]
print(a)
Output:
Tho
In (Membership operator): This operator is very useful in a search operation. If you want to search a string inside a text document this is very useful.
Example:
text = 'python is very good programming language'
if 'program' in text:
print('Text found')
else:
print('Text not found')
Output:
Text found
% (String Format): This is very useful to format the string.
Example:
print("My name is %s and i got %i marks" % ('sai', 90))
output:
My name is sai and i got 90 marks
String inbuilt Function
Operator |
Description |
upper() |
Converts all characters to uppercase in string |
lower() |
Converts all characters to lowercase in string |
replace(old, new [, max]) |
Replaces all occurrences of old in a string with new string |
join(seq) |
Concatenate all string representations of elements in sequence into a string, with separator string. |
split(str="", num=string.count(str)) |
It is used to split string |
splitlines( num=string.count('\n')) |
It is used to split lines in text |
count(str, beg= 0,end=len(string)) |
It counts how many times a string occurs in the original string. |
capitalize() |
It is used to capitalize the first letter of a string. |
center(width, fillchar) |
It is used to add width to string and fill some characters inside the width. |
find(str, beg=0 end=len(string)) |
It returns index value of a search string in original string else returns -1 |
rfind(str, beg=0,end=len(string)) |
Same as find() method. But search from the backwards string. |
index(str, beg=0, end=len(string)) |
It is used to find the index value of a character or string in the original string. |
rindex( str, beg=0, end=len(string)) |
It is used to find the last index of a character or string in original string |
encode(encoding='UTF-8',errors='strict') |
Returns encoded string version of string |
decode(encoding='UTF-8',errors='strict') |
Decodes the string using the codec registered for encoding |
strip([chars]) |
It is used to remove some extra characters in string |
lstrip() |
It is used to remove extra characters from the left side string. |
rstrip() |
It is used to remove extra characters from the right side string. |
swapcase() |
It converts lower character to upper character and upper character to lower character |
startswith(str, beg=0,end=len(string)) |
It is used to check the given string is started with the search string. |
endswith(suffix, beg=0, end=len(string)) |
It is used to check the given string is ended with the search string. |
title() |
It converts a sentence into title case |
maketrans() |
Returns a translation table to be used in translate function. |
ljust(width[, fillchar]) |
it Returns a space-padded string with the original string left-justified to a total of width columns. |
rjust(width,[, fillchar]) |
it Returns a space-padded string with the original string right-justified to a total of width columns. |
translate(table, deletechars="") |
It Returns a duplicate of the string in which each character has been mapped through the given interpretation table |
zfill (width) |
Pad a numeric string with zeros on the left, to fill a field of the predetermined width. The string is never truncated. |
expandtabs(tabsize=8) |
Return a duplicate of string where all tab characters are extended utilizing spaces. If tab size isn't given, a tab size of 8 characters is expected. |
isalnum() |
It checks only alpha (A-Z a-z) or numeric (0-9) character. It returns true if all characters are alphanumeric else return false even if one character is not alphanumeric. |
isalpha() |
It checks only alpha (A-Z a-z)character. It returns true if all characters are alpha else return false even if one character is not alpha. |
isdigit() |
It returns true if all characters are digits inside the string else returns false |
islower() |
It returns true if all characters are lower inside the string else return false |
isnumeric() |
It checks all characters are numeric or not. It returns true if all character numeric else returns false. |
isspace() |
It checks white spaces. If all characters are white spaces than it returns true else returns false. |
istitle() |
It returns true If a string is title cased else return false. |
isupper() |
If all characters are uppercase than it returns true else returns false. |
isdecimal() |
It returns true if a Unicode string contains only decimal characters else returns false |