Strings:
The string is a data type in Python. A string is a sequence of characters enclosed in quotes.
We can primarily, write a string in three ways:
Single quoted strings : a = ‘sri’
Double quoted strings : b = “sri”
Triple quoted strings : c = ‘’’ sri‘’’
String Slicing/Accessing Values in Strings:
To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example −
Index starts with 0 and goes till string (length - 1).
output:
H
ytho
Negative Indices: Negative indices can also be used .-1 corresponds to the (length-1) index, -2 to (length-2).
output:
!
o World!
Slicing with skip value:
We can provide a skip value as a part of our slice like this:
word = “amazing”
print(word[1:6:2] ) # It will return ’mzn’
Other advanced slicing techniques:
word = ‘amazing’
word[:7] or word[0:7] #It will return ‘amazing’
word[0:] or word[0:7] #It will return ‘amazing’
String Functions:
Some of the mostly used functions to perform operations on or manipulate strings are:
len() function : It returns the length of the string.
len(‘harry’) #Returns 5
endswith(“rry”) : This function tells whether the variable string ends with the string “rry” or not. If string is “harry”, it returns for “rry” since harry ends with rry.
count(“c”) : It counts the total number of occurrences of any character.
capitalize() : This function capitalizes the first character of a given string.
find(word) : This function finds a word and returns the index of first occurrence of that word in the string.
replace(oldword, newword) : This function replaces the old word with the new word in the entire string.
Escape Sequence Characters:
Sequence of characters after backslash ‘\’ [Escape Sequence Characters]
Escape Sequence Characters comprises of more than one character but represents one character when used within the string.
Examples: \n (new line), \t (tab), \’ (single quote), \\ (backslash), etc.
String Special Operators:
Assume string variable a holds 'Hello' and variable b holds 'Python', then −
Operator Description Example
+ Concatenation -
Adds values on either side of the operator a + b will give HelloPython
* Repetition -
Creates new strings, concatenating multiple copies of the same string a*2 will give -HelloHello
[] Slice -
Gives the character from the given index a[1] will give e
[ : ] Range Slice -
Gives the characters from the given range a[1:4] will give ell
in Membership -
Returns true if a character exists in the given string H in a will give 1
not in Membership -
Returns true if a character does not exist in the given string M not in a will give 1
r/R Raw String -
Suppresses actual meaning of Escape characters. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator, the letter "r," which precedes the quotation marks. The "r" can be lowercase (r) or uppercase (R) and must be placed immediately preceding the first quote mark. print r'\n' prints \n and print R'\n' prints \n
% Format -
Performs String formatting.
String Formatting Operator:
One of Python's coolest features is the string format operator %. Following is a simple example −
output:
My name is srinanda and my age is 33
List of formatters:
Format Symbol Conversion
%c character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase 'e')
%E exponential notation (with UPPERcase 'E')
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E
Practice:
Write a program to fill in a letter template given below with name and date.
letter = ‘’’ Dear <|NAME|>,
You are selected!
<|DATE|>
output:
Enter your Name:srinanda
Dear srinanda,
Yoy are selected!
2021-05-24
Write a program to detect double spaces in a string.
No comments:
Post a Comment