STRINGS IN PYTHON

Strings in python are type of datatype which is a set of contiguous characters represented in single or double quotes. Strings are similar to text. String(str) comes under sequence data type. sequence in common represent a group of elements.

Below are examples of strings

#Using double quotes to print string
print("learn python")

#Using to single quotes to print string
print('Learning python is fun')

#Using single quotes within double quotes
print("Python's string is easy to learn ")

#Using double quotes within single quotes
print('See the "double quotes" in strings')

#Concatenate two strings to print the results 
print("hello" + " world")

#Using variables to store the string values 
welcome = "hi"
name = "kishan"

print(welcome + ' ' + name)

#Using input to take a string and print the results
name1 = input("Please enter your name : ")
print(welcome + ' ' + name1)

#Concatenate two strings without using quotes in between
print(welcome+name)

Output:

C:\Users\kisha\IdeaProjects\first\venv\Scripts\python.exe C:/Users/kisha/IdeaProjects/createdb/strings.py
learn python
Learning python is fun
Python's string is easy to learn 
See the "double quotes" in strings
hello world
hi kishan
Please enter your name : kishan
hi kishan
hikishan

Process finished with exit code 0

Escape characters:

An escape character gets interpreted inside a single or double quotes. They are denoted by baskslash and can be used for various purpose

\a - bell or alert
\b - backspace
\f - formfeed
\n - newline
\r - carriage return
\s - space
\t - tab
\v - vertical tab
tabstring = "1\t2\t3\t4\t5"
print(tabstring)

print('the manager said "No, no, \'e\'s uh ,....he\'s resting".')

print("the manager said \"No,no, 'e's uh,.... he's resting\".")

print("""the manager said "No, no, 'e's uh,....he's resting". """)

splitstring = """ This string has been 
split over 
several
lines"""
print(splitstring)
splitstring1 = """This string \
is split \
over \
multiple lines """
print(splitstring1)

# print("C:\users\t k\t est.txt")
print("C:\\users\\tk\\test.txt")
print(r"C:\users\tk\test.txt")

Error:

  File "C:/Users/kisha/IdeaProjects/createdb/string1.py", line 20
    print("C:\users\t k\t est.txt")
         ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

O/P:

C:\Users\kisha\IdeaProjects\first\venv\Scripts\python.exe C:/Users/kisha/IdeaProjects/createdb/string1.py
1	2	3	4	5
the manager said "No, no, 'e's uh ,....he's resting".
the manager said "No,no, 'e's uh,.... he's resting".
the manager said "No, no, 'e's uh,....he's resting". 
 This string has been 
split over 
several
lines
This string is split over multiple lines 

Process finished with exit code 0

Sample output from console:

Strings can be sliced using the sqaure [] brackets to return bits and pieces of a string. The index of a string starts from ‘0’ in python. We will see slices in detail in another post.In below example , the strings are sliced with the index values to get a substring from a string

Eg:

stri = "i love python"
print(stri[0])
print(stri[7:13])

O/P:

C:\Users\kisha\IdeaProjects\first\venv\Scripts\python.exe C:/Users/kisha/IdeaProjects/createdb/string2.py
i
python

String formatting operators:

String formatting functions:

Some of the most commonly used string functions are

  • len
  • upper
  • lower
  • swapcase
  • capitalize
  • title
  • lstrip
  • rstrip
  • strip
  • max
  • min
  • replace
  • find
  • startswith
  • isdecimal
  • isalpha
  • isdigit
  • islower
  • isupper
  • …………

We will see the individual function in detail in another article

stri = "i love python"
print(len(stri))
print(stri.upper())
print(stri.lower())
print(stri.swapcase())
print(stri.capitalize())
print(stri.title())
print(stri.lstrip())

O/P:

I LOVE PYTHON
i love python
I LOVE PYTHON
I love python
I Love Python
i love python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s