Now we discuss string in detail. We shall discuss what are strings, how we can create multiline strings, string arrays, and much more.
How to print Strings Using print() Function in Python
Strings are textual value / message in which we can use alphabets , numbers & special character also. In python string can be made or create using single quotes or you can also use double quotes see example below:
#You can use double or single quotes: print("Codelikechamp.com") print('Codelikechamp.com')
Output
Codelikechamp.com Codelikechamp.com
How to Assign String to a Variable in Python
Now above we have just print a string through print() function but we can also assign a string to a variable as a value :
a = "Codelikechamp.com" print(a)
Output
Codelikechamp.com
How to create Multiline Strings in Python
We can create multiline string by using 3 rather it is single or double quotes:
#using 3 double quotes s = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.""" print(s)
Output
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
or you can also do this by single quotes like this:
#using 3 single quotes s = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''' print(s)
Output
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Are strings are arrays in Python
As we have studied that strings are text values in python means it is combination of words so it is similar like list that we can access each member of list so we can also access each character of a string and display it or what ever we want to do with that character see example below:
a = "codelikechamp" print(a[0]) print(a[1]) print(a[2]) print(a[3]) print(a[4]) print(a[5])
Output
c o d e l i
How can we do Looping through a string in Python
As we know strings are arrays so if we want to print all members or you can say character of the particular strings so what we can do we can iterate through a string using for loop:
for x in "codelikechamp": print(x)
Output
c o d e l i k e c h a m p
How can we Find Length of our string in Python
If we want to know how many letters or you can say characters in our string we can use len() function for this purpose see example:
a = "codelikechamp" print(len(a))
Output
13
So we see that there are total 13 character in our variable a
How can we Check phrase or character is present in a string in Python
I we want to check that a phrase or character is present in our string or not. We can do this by in function see example:
txt = "codelikechamp is best python learning platform" print("best" in txt)
Output
True
How can we do Slicing of string in Python
So as like in list we can retrieve data or record from specific range of our choice so as string are also list and arrays so we can same do in strings so this is called slicing to extract certain range of character from any text see example below:
b = "codelikechamp" print(b[2:5]) print(b[1:5]) print(b[0:5])
Output
del odel codel
How can we do Slicing from start of string in Python
To start indexing from start or beginning of our text we can do that by leaving the start index see example below:
b = "codelikechamp.com" print(b[:10])
Output
codelikech
How can we do Slicing from end of string in Python
By leaving out the end index, the range will go to the end of given text or string:
b = "codelikechamp.com" print(b[5:])
Output
ikechamp.com
How can we do negative Indexing in Python
We can do negative indexing to start the slice from the end of the string see example below:
b = "codelikechamp" print(b[-5:-2])
Output
cha
How to Modify Strings in Python
If we want to modify string to our choice means changing our lower case to string to upper case and vice versa , or remove white spaces in start or ending of our string or to replace string in our website we can do using a simple code in python see examples below:
Upper Case
a = "codelikechamp.com" print(a.upper())
Output
CODELIKECHAMP.COM
Upper Case
a = "CODELIKECHAMP.COM" print(a.lower())
Output
codelikechamp.com
Remove White-spaces in a string
a = " codelikechamp.com " print(a.strip())
Output
codelikechamp.com
Replace a string in Python
a = "codelikechamp" print(a.replace("champ", "hero")) print(a.replace("p", "mp"))
Output
codelikehero codelikechammp
Split string in Python
a = "codelikechamp , man" b = a.split(",") print(b)
Output
['codelikechamp ', ' man']
How to Concatenate a String in Python
Now let see how can we concatenate string mean to join or combine string okay see below example
a = "codelike" b = "champ" c = a + b print(c)
Output
codelikechamp
to add space between our string we can also do that
a = "codelike" b = "champ" c = a + " " + b print(c)
Output
codelike champ
Link:Â https://Codelikechamp.com
Medium Link: Follow me on Medium
Linkedin Link: Follow me on Linkedin