Strings and Characters

In Python, a string is a sequence of characters enclosed in single quotes, double quotes, or triple quotes. Strings are the immutable data type in Python. Strings are very important because most of the data that we use in daily life will be in the form of strings.

For example:

my_string = 'Hello, World!'

In this example, my_string is a string variable that contains the text “Hello, World!”.

Strings in Python are immutable, which means that once a string is created, its contents cannot be changed. However, you can create a new string by concatenating or slicing the original string.

You can access individual characters in a string by using indexing. In Python, indexing starts at 0. For example:

my_string = 'Hello, World!'
print(my_string[0])  # Output: H
print(my_string[4])  # Output: o

You can also access a range of characters in a string by using slicing. Slicing is done using the colon : operator. For example:

my_string = 'Hello, World!'
print(my_string[0:5])  # Output: Hello

In this example, the slice 0:5 returns the characters from index 0 up to, but not including, index 5.

Python provides many built-in methods for working with strings, such as upper(), lower(), split(), and join(). Here are a few examples:

my_string = 'Hello, World!'
print(my_string.upper())   # Output: HELLO, WORLD!
print(my_string.lower())   # Output: hello, world!
print(my_string.split(','))  # Output: ['Hello', ' World!']
print('-'.join(['Hello', 'World']))  # Output: Hello-World

In the first example, the upper() method returns a new string with all characters in uppercase.

In the second example, the lower() method returns a new string with all characters in lowercase.

In the third example, the split() method splits the string into a list of substrings based on a delimiter (a comma in this case).

In the fourth example, the join() method concatenates a list of strings into a single string, with a separator (- in this case) between each string.

Escape Characters and their Meanings

Escape CharactersMeanings
\aBell or alert
\nNew Line
\tHorizontal tab space
\vVertical tab space
\rEnter Button
\xCharacter x
\ \Displays single \

Raw String

In order to nullify the effect of escape characters, you can create a raw string by adding ‘r’ before the string. Raw strings take escape characters like \t or \n etc. as ordinary characters in a string and display the string with escape string in it.

str1 = r"Welcome to the \nShare Query\nTutorial"

Welcome to the \nShare Query\nTutorial

How to calculate length of a String?

A string length is measured by the number of characters in it. In python, there is len() function to find out the length of a string. len() function returns the number of characters including spaces in a string.

str1 = 'Python Tutorial - Strings and Characters'


Indexing in Strings

Index represents position of an individual character or elements in a string. Index is written using square braces[]. By specifying the position number in a string, you can get the individual element or character of a string.

str[0] represents the 0th element in a string.
str[1] represents the 1th elements in a string.

You can use negative index number, in order to get the element or character in reverse order.

str[-1] represents the last element.
str[-2] represents the second element.

Slicing the Strings

Python allows a string slicing by specifying start and the end index in the syntax given below:

  • Default step size is considered 1.
  • If ‘start’ and ‘stop’ value not provied in the slicing syntax. Then slicing is done from 0th to (n-1)th elements.
str = 'Learn Python'

str = 'Learn Python'

str = 'Learn Python'

str = 'Learn Python'

str = 'Learn Python'

str = 'Learn Python'

Learn Python
arn Python

A sub string can be extracted from a string by using reverse slicing.

str = 'Learn Python'

str = 'Learn Python'

str = 'Learn Python'

str = 'Learn Python'

str = 'Learn Python'

nhy r
nothyP nraeL

Repeating the Strings

A string can be repeated n times by using the ‘*’ operator.

str = 'Learn Python'

Learn PythonLearn Python

Also, chunk of a string can be repeated n times.

str = 'Learn Python'
sub_str = str[6::]


Concatenation of Strings

By using the concatenation operator (+) also called addition operator when used in numbers addition, can concatenate strings together.

str1 = 'Welcome to the Share Query. '
str2 = 'Learn Python'


Welcome to the Share Query Learn Python

Checking membership

In python, we can check if a string or character exist in the another string by using the ‘in’ and ‘not in’ operators. The operators ‘in’ and ‘not in’ operators make case-sensitive comparisons. So, upper case and lower case letters or strings compared differently.

#A python program to check whether a sub string is a part of main string.

main_str    = 'This is a Python Tutorial Website.'
sub_str     = 'Python'

if sub_str in main_str:
        print(sub_str+' is found in main string.')
        print(sub_str+' is not found in the main string.')

Python is found in main string.
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial