Python Strings
Python String can be defined under a single(' ') and double quotation("") marks.
Here some statements which are used to show, how to use python strings within single and double quotation marks.
‘Welcome to the codersarts’ and “Welcome to the codersarts”
Both give the same output result.
Working with python string
Here we work with python string so that you can easily understand it.
Using print statements
>>> print(‘Welcome to the codersarts’)
>>> print(“Welcome to the codersarts”)
Assigning to the variable
We can assign string to the variable as per given below example.
>>> a = “Welcome to the Codersarts”
Thanks for reading this.
In the next tutorial, we will learn about “python Operator”
Multiline String
It can be assigned to the variable using the three quotes.
a = """Hi,
welcome to the,
codersarts,
tutorials."""
print(a)
String is a type Array
In Python, strings are arrays of bytes representing unicode character.
Square brackets are used to access the strings element using the array.
a ="welcome to the codersarts tutorials."
print(a[2])
How to slice the strings?
In Python, strings slicing is done using the colon.
a ="welcome to the codersarts tutorials."
print(a[5 : 8])
It read the character from 5 to 7, hence the result is:
"me "
Negative Indexing
In Python, negative indexing starts from the end.
a ="welcome to the codersarts tutorials."
print(a[-8 : -3])
It read the character from -3 to -8 from the end, hence the result is:
"toria"
Finding the Length of string
To finding the length of the string we are using the len(), functions.
a ="welcome to the codersarts tutorials."
print(len(a))
String methods in python
There are different types of string methods is used in python. Here some important string methods are:
-
strip(): It used to remove white space from the beginning and start of the string.
-
lower(): It used to return the string in lower case.
-
upper(): It used to return the string in upper case.
-
replace(): It used to replace the string with another string.
-
split(): It used to split the string as per separator.
a =" Welcome to the Codersarts Tutorials."
print(a.strip())
It returns the result without space after removing it.
result is: "Welcome to the Codersarts Tutorials."
a ="Welcome to the Codersarts Tutorials."
print(a.lower())
It returns all the characters in the lowercase.
result is: "welcome to the codersarts tutorials."
a ="Welcome to the Codersarts Tutorials."
print(a.upper())
It returns all the characters in the uppercase.
result is: "WELCOME TO THE CODERSARTS TUTORIALS."
a ="Welcome, to the Codersarts, Tutorials."
print(a.split(","))
It returns the string after the split it with seperator
result is:['Welcome', ' to the Codersarts', 'Tutorials.']
a ="Welcome, to the Codersarts, Tutorials."
print(a.replace("W", "J"))
It replaces "W" with the "J"
result is:['Jelcome', ' to the Codersarts', 'Tutorials.']
String Checking
In Python, the string is checked using "in" and "not in". And it returns true if exist and false if not exist in string.
a ="Welcome, to the Codersarts, Tutorials."
x = "the" in a
print(x)
How to concatenate strings?
In Python, the strings are concatenate using the plus(+) sign.
a = "Welcome to the"
b = "Codersarts"
c = a + b
print(c)
It returns the:
"Welcome to the Codersarts"
String Format in Python
In Python, combine strings and numbers by using the format() method.
id = 115
txt = "Codersarts, id is {}"
print(txt.format(id))
or
name = "naveen"
id = "115"
a = "my name is {} and my emp id is {}"
print(a.format(name, id))
We can also use the curly bracket with the integer value to define the fixed positions.
name = "naveen"
id = 115
a = "my name is {0} and my emp id is {1}"
print(a.format(name, id))
Escape Characters in python
Python support multiple escapes characters, here below some important escape characters:
When inserting character is illegal to insert the string then we use escape character to insert the string.
In python escape, the character is defined by backslash \ and it followed by characters.
Escape character codes
-
\' (Single Quote)
-
\\ (Backslash)
-
\n (New Line)
-
\r (Carriage Return)
-
\t (Tab)
-
\b (Backspace)
-
\f (Form Feed)
-
etc
txt = 'Welcome to the coder\'sarts.'
print(txt)