What Is String in Python?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Python Strings

What is String in Python?


A string is a sequence of characters.

A character is simply a symbol. For example, the English language has 26


characters.

Computers do not deal with characters, they deal with numbers (binary). Even
though you may see characters on your screen, internally it is stored and
manipulated as a combination of 0's and 1's.

This conversion of character to a number is called encoding, and the reverse


process is decoding. ASCII and Unicode are some of the popular encoding used.

In Python, string is a sequence of Unicode character. Unicode was introduced to


include every character in all languages and bring uniformity in encoding. You
can learn more about Unicode from here.

How to create a string in Python?


Strings can be created by enclosing characters inside a single quote or double
quotes. Even triple quotes can be used in Python but generally used to represent
multiline strings and docstrings.

 script.py
# all of the following are equivalent

my_string = 'Hello'

print(my_string)

my_string = "Hello"

print(my_string)

my_string = '''Hello'''

print(my_string)

# triple quotes string can extend multiple lines

my_string = """Hello, welcome to

the world of Python"""

print(my_string)
When you run the program, the output will be:

Hello
Hello
Hello
Hello, welcome to
the world of Python

How to access characters in a string?


We can access individual characters using indexing and a range of characters using
slicing. Index starts from 0. Trying to access a character out of index range will raise
an IndexError. The index must be an integer. We can't use float or other types, this
will result into TypeError.

Python allows negative indexing for its sequences.

The index of -1 refers to the last item, -2 to the second last item and so on. We can
access a range of items in a string by using the slicing operator (colon).

 script.py
str = 'programiz'

print('str = ', str)

#first character

print('str[0] = ', str[0])

#last character

print('str[-1] = ', str[-1])

#slicing 2nd to 5th character

print('str[1:5] = ', str[1:5])

#slicing 6th to 2nd last character

print('str[5:-2] = ', str[5:-2])

If we try to access index out of the range or use decimal number, we will get errors.

1. # index must be in range


2. >>> my_string[15]
3. ...
4. IndexError: string index out of range
5.
6. # index must be an integer
7. >>> my_string[1.5]
8. ...
9. TypeError: string indices must be integers

Slicing can be best visualized by considering the index to be between the elements
as shown below.

If we want to access a range, we need the index that will slice the portion from the
string.

How to change or delete a string?


Strings are immutable. This means that elements of a string cannot be changed
once it has been assigned. We can simply reassign different strings to the same
name.

1. >>> my_string = 'programiz'


2. >>> my_string[5] = 'a'
3. ...
4. TypeError: 'str' object does not support item assignment
5. >>> my_string = 'Python'
6. >>> my_string
7. 'Python'
We cannot delete or remove characters from a string. But deleting the string entirely
is possible using the keyword del.
1. >>> del my_string[1]
2. ...
3. TypeError: 'str' object doesn't support item deletion
4. >>> del my_string
5. >>> my_string
6. ...
7. NameError: name 'my_string' is not defined

Python String Operations


There are many operations that can be performed with string which makes it one of
the most used datatypes in Python.
Concatenation of Two or More Strings

Joining of two or more strings into a single one is called concatenation.

The + operator does this in Python. Simply writing two string literals together also
concatenates them.
The * operator can be used to repeat the string for a given number of times.
 script.py
str1 = 'Hello'

str2 ='World!'

# using +

print('str1 + str2 = ', str1 + str2)

# using *

print('str1 * 3 =', str1 * 3)

Writing two string literals together also concatenates them like + operator.

If we want to concatenate strings in different lines, we can use parentheses.

1. >>> # two string literals together


2. >>> 'Hello ''World!'
3. 'Hello World!'
4.
5. >>> # using parentheses
6. >>> s = ('Hello '
7. ... 'World')
8. >>> s
9. 'Hello World'

Iterating Through String


Using for loop we can iterate through a string. Here is an example to count the
number of 'l' in a string.
 script.py
count = 0

for letter in 'Hello World':

if(letter == 'l'):

count += 1

print(count,'letters found')
String Membership Test
We can test if a sub string exists within a string or not, using the keyword in.
1. >>> 'a' in 'program'
2. True
3. >>> 'at' not in 'battle'
4. False

Built-in functions to Work with Python

Various built-in functions that work with sequence, works with string as well.

Some of the commonly used ones are enumerate() and len().


The enumerate() function returns an enumerate object. It contains the index and
value of all the items in the string as pairs. This can be useful for iteration.
Similarly, len() returns the length (number of characters) of the string.
 script.py
str = 'cold'

# enumerate()

list_enumerate = list(enumerate(str))

print('list(enumerate(str) = ', list_enumerate)

#character count

print('len(str) = ', len(str))

Python String Formatting


Escape Sequence
If we want to print a text like -He said, "What's there?"- we can neither use single
quote or double quotes. This will result into SyntaxError as the text itself contains
both single and double quotes.
1. >>> print("He said, "What's there?"")
2. ...
3. SyntaxError: invalid syntax
4. >>> print('He said, "What's there?"')
5. ...
6. SyntaxError: invalid syntax

One way to get around this problem is to use triple quotes. Alternatively, we can use
escape sequences.
An escape sequence starts with a backslash and is interpreted differently. If we use
single quote to represent a string, all the single quotes inside the string must be
escaped. Similar is the case with double quotes. Here is how it can be done to
represent the above text.

 script.py
# using triple quotes

print('''He said, "What's there?"''')

# escaping single quotes

print('He said, "What\'s there?"')

# escaping double quotes

print("He said, \"What's there?\"")

Here is a list of all the escape sequence supported by Python.

Escape
Sequence Description

\newline Backslash and newline ignored

\\ Backslash

\' Single quote

\" Double quote

ASCII Bell
\a

\b ASCII Backspace

\f ASCII Formfeed

\n ASCII Linefeed

\r ASCII Carriage Return


\t ASCII Horizontal Tab

\v ASCII Vertical Tab

\ooo Character with octal value ooo

\xHH Character with hexadecimal value HH

Escape Sequence in Python

Here are some examples

1. >>> print("C:\\Python32\\Lib")
2. C:\Python32\Lib
3.
4. >>> print("This is printed\nin two lines")
5. This is printed
6. in two lines
7.
8. >>> print("This is \x48\x45\x58 representation")
9. This is HEX representation

Raw String to ignore escape sequence


Sometimes we may wish to ignore the escape sequences inside a string. To do this
we can place r or R in front of the string. This will imply that it is a raw string and any
escape sequence inside it will be ignored.
1. >>> print("This is \x61 \ngood example")
2. This is a
3. good example
4. >>> print(r"This is \x61 \ngood example")
5. This is \x61 \ngood example

The format() Method for Formatting Strings


The format() method that is available with the string object is very versatile and
powerful in formatting strings. Format strings contains curly braces {} as
placeholders or replacement fields which gets replaced.

We can use positional arguments or keyword arguments to specify the order.


 script.py
# default(implicit) order

default_order = "{}, {} and {}".format('John','Bill','Sean')

print('\n--- Default Order ---')

print(default_order)

# order using positional argument

positional_order = "{1}, {0} and {2}".format('John','Bill','Sean')

print('\n--- Positional Order ---')

print(positional_order)

# order using keyword argument

keyword_order = "{s}, {b} and {j}".format(j='John',b='Bill',s='Sean')

print('\n--- Keyword Order ---')

print(keyword_order)

The format() method can have optional format specifications. They are separated


from field name using colon. For example, we can left-justify <, right-justify > or
center ^ a string in the given space. We can also format integers as binary,
hexadecimal etc. and floats can be rounded or displayed in the exponent format.
There are a ton of formatting you can use. Visit here for all the string formatting
available with the format() method.
1. >>> # formatting integers
2. >>> "Binary representation of {0} is {0:b}".format(12)
3. 'Binary representation of 12 is 1100'
4.
5. >>> # formatting floats
6. >>> "Exponent representation: {0:e}".format(1566.345)
7. 'Exponent representation: 1.566345e+03'
8.
9. >>> # round off
10. >>> "One third is: {0:.3f}".format(1/3)
11. 'One third is: 0.333'
12.
13. >>> # string alignment
14. >>> "|{:<10}|{:^10}|{:>10}|".format('butter','bread','ham')
15. '|butter | bread | ham|'

Old style formatting


We can even format strings like the old sprintf() style used in C programming
language. We use the % operator to accomplish this.
1. >>> x = 12.3456789
2. >>> print('The value of x is %3.2f' %x)
3. The value of x is 12.35
4. >>> print('The value of x is %3.4f' %x)
5. The value of x is 12.3457

Common Python String Methods


There are numerous methods available with the string object. The format() method
that we mentioned above is one of them. Some of the commonly used methods
are lower(), upper(), join(), split(), find(), replace() etc. Here is a complete list
of all the built-in methods to work with strings in Python.
1. >>> "PrOgRaMiZ".lower()
2. 'programiz'
3. >>> "PrOgRaMiZ".upper()
4. 'PROGRAMIZ'
5. >>> "This will split all words into a list".split()
6. ['This', 'will', 'split', 'all', 'words', 'into', 'a', 'list']
7. >>> ' '.join(['This', 'will', 'join', 'all', 'words', 'into', 'a',
'string'])
8. 'This will join all words into a string'
9. >>> 'Happy New Year'.find('ew')
10. 7
11. >>> 'Happy New Year'.replace('Happy','Brilliant')
12. 'Brilliant New Year'

Python Data Type: String - Exercises


Calculate the length of a string
def string_length(str1):

count = 0

for char in str1:

count += 1

return count

print(string_length('w3resource.com'))

Count the number of characters (character


frequency) in a string
Sample String : google.com'
Expected Result : {'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1}
def char_frequency(str1):
dict = {}

for n in str1:

keys = dict.keys()

if n in keys:

dict[n] += 1

else:

dict[n] = 1

return dict

print(char_frequency('google.com'))

Copy
Sample Output:
{'o': 3, '.': 1, 'g': 2, 'l': 1, 'e': 1, 'c': 1, 'm': 1}

Takes a list of words and returns the


length of the longest one
def find_longest_word(words_list):

word_len = []

for n in words_list:

word_len.append((len(n), n))

word_len.sort()

return word_len[-1][1]

print(find_longest_word(["PHP", "Exercises", "Backend"]))

Copy
Sample Output:
Exercises
Get a string made of its first three
characters of a specified string
Sample function and result :
first_three('ipy') -> ipy
first_three('python') -> pyt

def first_three(str):
return str[:3] if len(str) > 3 else str

print(first_three('ipy'))
print(first_three('python'))
print(first_three('py'))

Sort a string lexicographically


def lexicographi_sort(s):

return sorted(sorted(s), key=str.upper)

print(lexicographi_sort('w3resource'))

print(lexicographi_sort('quickbrown'))

Copy
Sample Output:
['3', 'c', 'e', 'e', 'o', 'r', 'r', 's', 'u', 'w']
['b', 'c', 'i', 'k', 'n', 'o', 'q', 'r', 'u', 'w']

Format a number with a percentage


x = 0.25

y = -0.25

print("\nOriginal Number: ", x)

print("Formatted Number with percentage: "+"{:.2%}".format(x));

print("Original Number: ", y)


print("Formatted Number with percentage: "+"{:.2%}".format(y));

print()

Copy
Sample Output:
Original Number: 0.25
Formatted Number with percentage: 25.00%
Original Number: -0.25
Formatted Number with percentage: -25.00%

Count occurrences of a substring in a


string
str1 = 'The quick brown fox jumps over the lazy dog.'

print()

print(str1.count("fox"))

print()

Copy
Sample Output:
1

Reverse a string

def reverse_string(str1):

return ''.join(reversed(str1))

print()

print(reverse_string("abcdef"))

print(reverse_string("Python Exercises."))

print()
Copy
Sample Output:
fedcba
.sesicrexE nohtyP

Reverse words in a string

def reverse_string_words(text):

for line in text.split('\n'):

return(' '.join(line.split()[::-1]))

print(reverse_string_words("The quick brown fox jumps over the lazy dog."))

print(reverse_string_words("Python Exercises."))

Copy
Sample Output:
dog. lazy the over jumps fox brown quick The
Exercises. Python

Print the index of the character in a string


Expected output:
Current character w position at 0
Current character 3 position at 1
Current character r position at 2
-------------------------
Current character c position at 8
Current character e position at 9
str1 = "w3resource"

for index, char in enumerate(str1):

print("Current character", char, "position at", index )


Copy
Sample Output:
Current character w position at 0
Current character 3 position at 1
Current character r position at 2
Current character e position at 3
Current character s position at 4
Current character o position at 5
Current character u position at 6
Current character r position at 7
Current character c position at 8
Current character e position at 9

Check if a string contains all letters of the


alphabet
import string

alphabet = set(string.ascii_lowercase)

input_string = 'The quick brown fox jumps over the lazy dog'

print(set(input_string.lower()) >= alphabet)

input_string = 'The quick brown fox jumps over the lazy cat'

print(set(input_string.lower()) >= alphabet)

Copy
Sample Output:
True
False

Convert a string in a list

str1 = "The quick brown fox jumps over the lazy dog."

print(str1.split(' '))

str1 = "The-quick-brown-fox-jumps-over-the-lazy-dog."

print(str1.split('-'))
Copy
Sample Output:
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the',
'lazy', 'dog.']
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the',
'lazy', 'dog.']

Count and display the vowels of a given


text
def vowel(text):

vowels = "aeiuoAEIOU"

print(len([letter for letter in text if letter in vowels]))

print([letter for letter in text if letter in vowels])

vowel('w3resource');

Copy
Sample Output:
4
['e', 'o', 'u', 'e']

Remove spaces from a given string


def remove_spaces(str1):

str1 = str1.replace(' ','')

return str1

print(remove_spaces("w 3 res ou r ce"))

print(remove_spaces("a b c"))
Find the maximum occurring character in
a given string
def get_max_occuring_char(str1):

ASCII_SIZE = 256

ctr = [0] * ASCII_SIZE

max = -1

ch = ''

for i in str1:

ctr[ord(i)]+=1;

for i in str1:

if max < ctr[ord(i)]:

max = ctr[ord(i)]

ch = i

return ch

print(get_max_occuring_char("Python: Get file creation and modification


date/times"))

print(get_max_occuring_char("abcdefghijkb"))

Copy
Sample Output:
t
b

Compute sum of digits of a given string


def sum_digits_string(str1):
sum_digit = 0
for x in str1:
if x.isdigit() == True:
z = int(x)
sum_digit = sum_digit + z

return sum_digit

print(sum_digits_string("123abcd45"))
print(sum_digits_string("abcd1234"))

Count Uppercase, Lowercase, special


character and numeric values in a given
string
def count_chars(str):

upper_ctr, lower_ctr, number_ctr, special_ctr = 0, 0, 0, 0

for i in range(len(str)):

if str[i] >= 'A' and str[i] <= 'Z': upper_ctr += 1

elif str[i] >= 'a' and str[i] <= 'z': lower_ctr += 1

elif str[i] >= '0' and str[i] <= '9': number_ctr += 1

else: special_ctr += 1

return upper_ctr, lower_ctr, number_ctr, special_ctr

str = "@W3Resource.Com"

print("Original Substrings:",str)

u, l, n, s = count_chars(str)

print('\nUpper case characters: ',u)

print('Lower case characters: ',l)

print('Number case: ',n)

print('Special case characters: ',s)

Copy
Sample Output:
Original Substrings: @W3Resource.Com
Upper case characters: 3
Lower case characters: 9
Number case: 1
Special case characters: 2

You might also like