Python Strings
String is combination of character sequence, and symbol of unicode character. string can be defined of within the enclosed quotes. for example
'this is string within single quotes'
"this is string within double quotes"
'''this is string within triple single quotes'''
"""this is string within triple double quotes"""
Define a string text within the quotes. python are provides flexibility to define a string in multiple way. we can use anyone of them. Note that when using of python shell to execute this given string statement that are produced the following result.
>>> 'this is string within single quotes'
'this is string within single quotes'
>>> "this is string within double quotes"
'this is string within double quotes'
>>> '''this is string within triple single quotes'''
'this is string within triple single quotes'
>>> """this is string within triple double quotes"""
'this is string within triple double quotes'
Observe that result of every string are printed in single quotes. but this result are produce by python command line. but if save this file as sources code. and then run by using of this command "python file.py" then this will not produce any result.
So in this situation initialize this string value to variable. or can use the print() function to display its value. for example.
#Case 1 : Assign value to variable
#assign the string value to variable
stringText="String Value"
#Case 2 : display normal text
# display user message
print("Simple Text") #display text
print(stringText) #display variable value
Output
Simple Text
String Value
Case 1 are used to assign string value to variable. the advantage of this method is we can use this variable value at any place in the program. and there are also possible to modify string text as well.
Case 2 is constant string that are used to print simple result or message.
In above example we are defining of string value in a four way. within single quotes, within double quotes, within triple single or triple double quotes. but that is not possible to start the string with single quotes and ending with double quotes. or vice versa. see example.
#invalid string define
text='Invalid string"
text="Invaild Text'
Error
File "test.py", line 1
text='Invalid string"
^
SyntaxError: EOL while scanning string literal
So define a string only within similar quotes.
String indexing
Base index of string is start by 0. that are similar to c,c++ and java. let see one example.
#simple example of string
textValue="Simple Text String"
print(textValue)
Output
Simple Text String
View all index of given this string

So we can access string value at specified index. index are unique identifier of string text. see this example.
#simple example of string
textValue="Simple Text String"
print(textValue[0]) # S
print(textValue[1]) # i
print(textValue[2]) # m
print(textValue[7]) # T
print(textValue[8]) # e
Output
S
i
m
T
e
Multiline String
There is various way to define multi line text. for example.
#define multiline string in python
#Case 1: Continuation of single line text
text="Welcome"\
" To"\
" Programming"
print(text)
#Case 2: Define single line text
text=("Single"
" Line"
" Text")
print(text)
#Case 3: define multiline text
#Using of double triple quotes
text="""PYthon Programming
Multi line text"""
print(text)
#Case 4: define multiline text
#Using of single triple quotes
text='''1'st line
2'nd line
3'rd line..'''
print(text)
Output
Welcome To Programming
Single Line Text
PYthon Programming
Multi line text
1'st line
2'nd line
3'rd line..
String Available methods
There are various function are available to used in python string. there are follows.
len() function : This function are accepting one parameter value. this values can be normal string, string variable, tuple, set, dictionary etc. if providing a string or a string variable of this function. then it will returning a value of how many number of character in given string. for example.
#Assign string value
textValue="This is python String"
print(len(textValue)) #display length
Output
21
There are also possible to count length of string using logical and programmatically. there are various way, here given few example.
#Assign string value
count=0
textValue="This is python String"
for text in textValue:
count+=1
print("For loop result:" ,count)
Output
21
upper() function : this are returning the result of capitalized (uppercase) string. see example.
#Assign string value
textValue="This is python uppercase String"
print(textValue.upper()) #display length
Output
THIS IS PYTHON UPPERCASE STRING
We can also programmatically convert lowercase character to uppercase. for example
#Convert given string to uppercase Text
#Assign a string value
textValue="This is python String"
newText=''
count=0
for text in textValue:
#check the condition of lowercase character
if(text>='a' and text<='z'):
#ord() function are convert character to int
#chr() function are convert int to character
newText=newText+chr(ord(text)-32)
else:
#add a characte
newText=newText+text
#for index
count+=1
print("Before : "+textValue)
print("After : "+newText)
Output
Before : This is python String
After : THIS IS PYTHON STRING
Note that python string are immutable that are not allowed to replace characters text of at place. so we are solve this problem using another variable.
lower() function : This function is returning of all lower case string.
#define a string
text="WELCOME"
print(text.lower())
Output
welcome
Find text Contains: Find out specific text string contain given text portion are exist or not. We can solve this problem in many ways.
data="Python 3 Code"
# case 1 : using find method
# When find function are return -1
# That means given text sequence are not exist in given String
print(data.find("Code")) #Returning first index of matched text
# Case 2: Using in operator
if "Code" in data:
print("Code is found")
else:
print("Code is not found")
# Case 3 : Uisng str __contains__ method
print(str.__contains__(data,"Code"))
Output
9
Code is found
True
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment