top of page

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) 

Other String Methods in Python

In python different types of string methods are used, which we are discussed in below:

capitalize(): It converts the first character of the string into the uppercase.

 

Example:

a ="hello, codersarts"

print(a.capitalize())

 

Output:

Hello, codersarts

center(): It shifts the string with a specified digit.

Example:

string = "hi"
x = string.center(20)
print(x)

Output: return with specified space

.......................... hi

count(): count the how many numbers of times specified value appeared in the string.

Example:

string = "hi hi hi this is codersarts"
x = string.count("hi")
print(x)

Output: return with specified space

3

 

find(): Returns the position of text in the string.

 

Example:

string = "hi, this is codersarts"
x = string.find("is")
print(x)

Output: return with specified space

3

format(): In Python, combine strings and numbers by using the format() method. See the example above.

index(): It returns the position of a specified character or word in the string which occurs first.

 

Example:

string = "hi, this is codersarts"
x = string.index("is")
print(x)

Output: 

6

 

isalnum(): Check all the characters in the string is alphanumeric or not, return true or false

Example:

string = "hi12"
x = string.isalnum()
print(x)

Output: 

True

isalpha(): It used to check the all the character in string is alpha.

Example:

string = "hi12"
x = string.isalpha()
print(x)

Output: 

False

isdigit(): It is used to check the given value is digit or not.

Example:

string = "12"
x = string.isalpha()
print(x)

Output: 

True

 

islower(): It is used to check the given value or words in the string  is lowercase or not.

 

Example:

string = "hi!, this is codersarts"
x = string.islower()
print(x)

Output: 

True

Other more pyton methods which is useful are :

 

isnumeric(), isspace(), isupper(), join(), lower(), partition(), replace(), rsplit(), rstrip(), split(), title(), upper(), etc.

bottom of page