Introduction to Python String Concept

In this article you will learn about Python String.

Python String – Before moving ahead, let’s know a little about Python.

Introduction – Python is open-source and free programming language. It is easy to use and learn as compared to other languages like C ++, Java, C# and JavaScript. It is highly recommended and allows itself to get integrated with other languages like C ++, C#, etc., One of the most important key factors of Python is it is Object-Oriented Language and had been used by many giant companies for so many purposes such as handling file data, web app, Desktop application, Machine learning and so on.

Python String – The string module in Python contains sequence of characters in string. String is something which is written inside single or double quotations. It doesn’t matter to Python whether it’s int or float, if it’s written inside quotations then it is known as String. Python’s built-in string supports the sequence type methods such as list, tuple, set and dictionary etc.

Example – It returns string inside single and double quotation marks.

x = 'Hello, Python world!'
z = "Hello, Python!"

print(x)
print(z)
python-string-method

Assign string to a variable – Assign a string to a variable, randomly pick any variable name (left side) and assign it to a value (right side) by using equal sign in between them.

Example – It returns the value which is stored in the variable.

x = "Hello"
print(x)
python-string-method

Multiline String – It defined as using three (“””…”””)quotation marks. A string to a variable can be assigned by using three single or double quotation marks.

Example – It returns the value which is stored in the variable.

x = """Hello, Python world!""" # use of double three quotation.
y = '''Hello, Python world!''' # use of single three quotation.

print(x)
print(y)
python-string-method

String length – To get the length of the string use Python in-built function len().

Example – It returns the length of the string.

x = 'Hello, Python world!'
print('The length of variable x is', z)
python-string-method
As it is shown clearly that it executed length of whole variable including whitespace and punctuation). 

String as array – An array is a collection of variables that carries a single name. Arrays are same in string except few differences. A square can be used to access the elements of string.

Example – Get the character at position 4 (remember Python starts counting from 0)

x = 'Hello, Python world!'
y = x[4]

print('The letter at position 4 is', y)
python-string-method
As it is shown clearly that It executed letter ‘o’ at position 4.

String Slicing – It defined as getting back a group or range of character. Slicing in Python can be done using start index and the end index, separated by a colon to get return back a string.

Example – Get the character back from index number 7 to 13 (not included) and it returns “Python” as output.

x = 'Hello, Python world!'
y = (x[7:12])

print(y)
python-string-method
As it is shown clearly that it returned “Python” as output because word ‘Python’ is between range.

Negative Indexing – By using negative indexing, slicing can be done from end of string.

Example – Get the character back from index number 7 to 13 (not included) and it returns “Python” as output.

x = 'Hello, Python world!'
y = (x[-15:-8])
print(y)
As it is shown clearly that it returned “, Pytho” as output because word ‘Python’ is between range.

String Methods – Python has some in-built methods that can be used on strings.

Let’s begin with some of them and rest of will be discussed later in detail.

capitalize() method – It returns a copy of the string only with its first character is capitalized and rest lowercased.

Syntax - string.capitalize()
Parameter Values - No parameter values.

Example – It converts every first letter of word into capital form.

x = 'hello, python world!'
y = x.capitalize()

print(x)
print(y)
python-string-method

strip() method – The strip() method removes any whitespace from the beginning or at the end.

Syntax - string.strip()
Parameter Values - No parameter values.

Example – It removes unnecessary whitespace and returns string.

before_strip_method  = '      Hello, Python world!'
print('before_strip_method:', before_strip_method)

x = '                         Hello, Python world! '
after_strip_method = x.strip()

print('after_strip_method:', after_strip_method)
python-string-method
As it is shown clearly that first variable (before_strip_method) did not remove whitespace presented at the beginning of the string but second variable (after_strip_method) after using strip() method removed whitespace. 

count() method – It returns number of string of repeating characters in given string. It has also option to take an additional parameters as starting and ending to specify the starting and ending positions in the string respectively.

Note:  It should be noted that Python index starts from 0 not 1.

Example 1- It counts particular phrase repeating in given string.

x = 'Python in Python'
y = x.count('Python')

print("Name of character 'Python' contains ", y , "times in whole string.")
python-string-method
As it is shown clearly that count() method returned 2 as output because character ‘Python’ is written two times.

Example 2 – It counts particular letter repeating between index number in given string.

x = 'Python in Python'
y = x.count('o', 4,15) #count o from starting to till end.
print("Name of letter 'o' contains ", y , "times in character Python.")

a = 'codingstreets'
b = a.count('t', 6,12)
print("Name of letter 't' contains ", b , "times in character codingstreets.")
python-string-method
As it is shown clearly that letter ‘o’ in character ‘Python’ contains 2 times and ‘t’ in character ‘codingstreets’ contains 2 times.

Here counting starts from first ‘o’ that’s index number 4 and it ends before index number 15.

uppercase() method – It returns given string in upper case. In other words, every letter of string will be in capital letter or upper case.

Syntax - string.upper()
Parameter Values - No parameter values.

Example – It returns string in upper case.

x = 'Python in Python'
y = x.upper()
print(y)
python-string-method
As it is shown clearly that every letter in string executed in upper case.

Python check string – To check if particular character is present in string or not, use function ‘in’ or ‘not in’.

Example 1- Use of ‘in’ to check if word is present in given string.

x = 'Python in Python'
print('Python' in x)
python-string-method
As it is shown clearly that it returned True because word ‘Python’ is presented in variable name x.

Example 2- Use of ‘not in’ to check if word is present in given string.

x = 'Python in Python'
print('on' not in x)
python-string-method

As it is shown clearly that it returned False because word ‘on’ is presented in word ‘Python’ but according to line 3, word ‘on’ should not present in word ‘Python’.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Follow us

3 thoughts on “Introduction to Python String Concept”

Leave a Comment

Stay in the loop

codingstreets