PREV NEXT
- String is nothing but collection of sequence of character,whichever enclosed in single quotes(‘ ‘),double quotes (“ “) and triple quotes (”’ ”’)
- Python strings are immutable
- String can be a cobination of numbers and alphabates
- String can be a empty also ,that has 0
Example:
1 2 3 4 5 6 7 8 9 10 11 |
str1='I am single quote string' str2="I am double quote string" str3='''I am triple quote string''' print (str1) print (str2) print (str3) |
Output:
1 2 3 4 5 |
I am single quote string I am double quote string I am triple quote string |
Create empty string:
str1=”
str2=””
str3=”””
print (str1)
print (str2)
print (str3)
Output:
Note: it will print blanck on command prompt
Escape Sequence:
We can use escape sequence in string for special cases
Examples:
Escape Sequence | Meaning of ES |
\n | Newline – It will Prints a newline character |
\t | Tab – It will Prints a tab character |
\\ | Backslash – It will Prints a backslash ( \ ) character |
\’ | Single quote – It will Prints a single quote |
\” | Double quote – It Will Prints a double quote |
Example:
1 2 3 4 5 6 7 8 9 10 11 |
str1="I\'m a \"double quote string\" in double quotes" str2="I\'m a \'single quote string\' in double quotes" str3='d:\\user\\python' print (str1) print (str2) print (str3) |
Output:
1 2 3 4 5 |
I'm a "double quote string" in double quotes I'm a 'single quote string' in double quotes d:\user\python |
Access the individual characters in strings:
To Access the individual characters from string we can do it in two ways.They are
1.Forward Indexing
2.Backward Indexing
1.Forward Indexing:
P | Y | T | H | O | N |
Index 0 1 2 3 4 5
As per the defination of string it will stored in sequence .We can access the individual characters in string by using the string variable and index.As per above schematic we need to generalize that forward index will start from “0” th index and end with “n-1” index.To use the index we can use square brackets “[]”
Below is the example to access the charatcters in the string
course=”python”
course[0]=’p’
course[1]=’y’
course[2]=’t’
course[3]=’h’
course[4]=’o’
course[5]=’n’
Below is the example is showing how to iterate the each character by using loops
Example:
1 2 3 4 5 6 7 |
course="python" length=len(course) for i in range(length): print ("course[%d]:%s"%(i,course[i])) |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
course[0]:p course[1]:y course[2]:t course[3]:h course[4]:o course[5]:n |
2.Backward Indexing or negative indexing:
We can also use negative index in string to access the individual characters.Negative index will start from the last character ,it will start with “-1” and end with “-n”
P | Y | T | H | O | N |
Negative Index -6 -5 -4 -3 -2 -1
Below is the example to access the charatcters in the string
course=”python”
course[-6]=’p’
course[-5]=’y’
course[-4]=’t’
course[-3]=’h’
course[-2]=’o’
course[-1]=’n’
Example:
1 2 3 4 5 6 7 |
course="python" length=len(course) for i in range(1,length+1): print ("course[%d]:%s"%(-i,course[-i])) |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
course[-1]:n course[-2]:o course[-3]:h course[-4]:t course[-5]:y course[-6]:p |
String Slicing:
String slicing is used to get the set of characters at a time.To do this we can use the slicing operator(:)
Syntax:
string_name[startindex:endindex]
- Return:
It will return the slice of characters from the start index to end index,the character end index is not be include in the slice. - If endindex is greater than the string length then it will give the complete string from start index to complete stringNote: Here index is optional
Example:
1 2 3 4 5 6 7 |
string = "python" print string[0:3] print string[2:5] print string[2:len(string)+5] |
Output:
1 2 3 4 5 |
pyt tho thon |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
string = "python" print (string[:]) print (string[1:]) print (string[3:]) print (string[:1]) print (string[:3]) print (string[0:3]) print (string[2:5]) print (string[2:len(string)+5]) print (string[:-1]) print (string[-1:]) print (string[-3:-1]) print (string[-1:-3]) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
python ython hon p pyt pyt tho thon pytho n ho |
String Methods In Python:
By using string methods we can find the sub string in main string,convert string etc.
Syntax:
string_name.method_name(arg1,arg2…..argn)
Note:Here arguments are optinal
Methods for find and replace the strings:
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
string="i hi hello hai how are you?" string1="hello" print ("The string is find at:",string.find("h")) print ("The string is rfind at:",string.rfind("h")) print ("The string is find at:",string.find("hey")) print ("The string is endswith:",string1.endswith("lo")) print ("The string is startwith:",string1.startswith("he")) |
Output:
1 2 3 4 5 6 7 8 9 |
The string is find at: 2 The string is rfind at: 15 The string is find at: -1 The string is endswith: True The string is startwith: True |
Strings converting Methods:
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
string="i am leArNing pyThON" string1="\n\tNewline\tstring" print ("String in lower cases:",string.lower()) print ("String in upper cases:",string.upper()) print ("String in first char upper case:",string.capitalize()) print ("String in title:",string.title()) print ("String in swapcase:",string.swapcase()) print ("String in strip:",string1.strip()) print ("String in strip:",string1.strip('\t')) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
hakeem@hakeem:~/Hakeem/Network_Programming/PYTHON$ python3 str1.py String in lower cases: i am learning python String in upper cases: I AM LEARNING PYTHON String in first char upper case: I am learning python String in title: I Am Learning Python String in swapcase: I AM LEaRnING PYtHon String in strip: Newline string String in strip: Newline string |
Strings For Testcase:
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
str1="123" str2="hai" str3="hai123" str4="ABD" str5="abcd " str6=" " print ("isalnum is:",str1.isalnum()) print ("isalnum is:",str2.isalnum()) print ("isalpha is:",str2.isalpha()) print ("isalpha is:",str1.isalpha()) print ("isdigit is:",str1.isdigit()) print ("isdigit is:",str3.isdigit()) print ("islower is:",str2.islower()) print ("isupper is:",str4.isupper()) print ("isspace is:",str6.isspace()) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
isalnum is: True isalnum is: True isalpha is: True isalpha is: False isdigit is: True isdigit is: False islower is: True isupper is: True isspace is: True |
String Formats:
Example:
1 2 3 4 5 6 7 |
str1="hello" print("Center is:",str1.center(15)) print("ljust is:",str1.ljust(15)) print("rjust is:",str1.rjust(15)) |
Output:
1 2 3 4 5 |
Center is: hello ljust is: hello rjust is: hello |