1. Home
  2. Docs
  3. Python 101 – Python Programming for Beginners
  4. Strings

Strings

 
A string a sequence of characters. Strings are immutable objects

String is represented in quotes. Single, double or triple quotes.

Examples.
 
Ex 1.
 
String presented in single quotes.
 

'This is a string ' 

 
String presented in double quotes.
 

"This is also a string" 

 

Multi-line string

 

''' this is a multi-line

... string which continues on

... 3 lines with triple single quoates'''

output

' this is a multiline\nstring which countinues on \n3 lines '

 
Ex 2.
 

""" this is a multi-line

... string which continues on

... 3 lines with triple double quotes """

'c' #is also a string

'10' # even this is also string

 

Types of strings.

 
In Python there are three types of stings.

raw string, Unicode string and byte string.

Raw string ignore the escapes sequence in a given stings.

By adding r or R prefix to a string makes it a raw string.
 

raw_string = r'C:\\Users\\PC-USER'

 print(raw_string)

output:

C:\\Users\\PC-USER

 

Unicode strings

 
Unicode strings contains, UTF-8 character set.

u or U prefix is used to make Unicode string.

Byte string

‘b’ prefix is used to make a byte string.

b’string’

 

String concatenation.

 
Adding two strings is a concatenation operation.

“+”
plus operator is used for concatenation.
 

str1 = "Hello "

str2 = "There!"

str3 = str1 + str2

print(str3)

output:

Hello There!

 

Built-in function on strings

 

len()

 
Returns the length of the string.
 
String object doesn’t have its len() method.
 

text = "Python is a cool programming language"

len(text)

output:

37

 

max()

 
Returns the biggest item in the string.
 

max(text)

output:

'y'


 

min()

 
Returns the smallest item in the string.
 

min(text)

output:

' '   # in this case it's a space

 

Slicing and Indexing.

 
String is a sequence of character.

Elements in a strings are characters.

We can access character of string by using index operation.
 

Syntax: string[index]

 
Let’s take a sample string ‘text’ as below, and do some indexing.
 
text = “Python is a cool programming language”
 
Like array, we can access the first character by 0 the index,

and last character by len – 1 index.

 
Examples:
 

text = "Python is cool programming language"

print(text)

'Python is cool programming language'

text[0]

output:
'P'

output:

text[34]

output:

'e'

text[20]

'a'

 
Here text has 35 characters.
 
If you try to access beyond this, Python interpreter throws an IndexError.

note index stars from 0, and ends at length -1
 

text[35]

output:

Traceback (most recent call last):

File "", line 1, in

IndexError: string index out of range

 
Unlike in other programming languages,

In Python you can also use negative index.

negative index, allows us to access elements form end of list.
 

text[-1]

'e'

 text[-19]

'r'

text[-20]

'p'


 

Slicing

 
With slicing we can get a sub-string of string.

for slicing “:” colon is used inside [:] square brackets.

Syntax:
 

string[begin:end]


 
begin and end are indexes to get the sub-string.
 
‘begin’ default value is 0 and ‘end’ default value is length – l of the string.
 

Examples of string slicing

 
Ex 1:
 
if no ‘begin’ and ‘end’ indexes specified returns whole string.
 

text[:]

'Python is cool programming language'

 
Ex 2:
 
If only end is specified, begin defaults to zero.
 

text[:4]

'Pyth'

 
Ex 3:
 
If only end is not specified, returns string from 8th char till end.
 

text[7:]

'is cool programming language

 
Ex 4:
 
Get a specific sub-string ‘is’
 

text[7:9]

'is'

 
Ex 5:
 
Get a specific sub-string
 

text[15:]

'programming language'

 
Ex 6:
 
Using a negative index for end.
 

text[16:-9]

'rogramming'

text[15:-9]

'programming'

 
We can use a optional step parameter for slicing,

this is called extended slicing.
 

Extended slicing syntax.

 

[begin:end:step]

 
Ex 7:
 
Examples of extended slicing syntax.
 

text[:], text[::1] and text[::] are all same.

 text[:]

'Python is cool programming language'

text[::]

'Python is cool programming language'

text[::1]

'Python is cool programming language'

 
Ex 8:
 
Returns sub-string skipping every second character.
 

text[::2]

'Pto sco rgamn agae'

 
Ex 9:
 
Returns sub-string skipping every third character.
 

text[::3]

'Ph opgmnlgg'

 
Ex 10:
 
Reversing a string with slicing.
 

text[::-1]

'egaugnal gnimmargorp looc si nohtyP'

 
Ex 11:
 

Returns a string skipping every second character from backwards.
 

text[::-2]

'eaga nmagr ocs otP'

 

str() and int() type constructors.

 
Str() is a type constructor, with which numbers are converted to strings
 
At times, you want to concatenate strings with numbers.

As Python is a strongly typed language its throws a TypeError error,

Adding numbers and strings

 
if you try to add numbers to strings.
 
Example.
 
If want to have a string like
 

"10 apples"

num_apples = 10

str1 = "Apples"

num_apples + str1

output:

Traceback (most recent call last):

File "", line 1, in

TypeError: unsupported operand type(s) for +: 'int' and 'str'

 

To overcome this, use a built-in function str(),

on number before adding to another string.
 
Ex 1:
 

num_apples = 10

str1 = "Apples"

str(num_apples) + str1

output:

'10Apples'

Ex 2:
 
To get ” I ate 10 Apples”
 

num_apples = 10

str1 = "I ate "

str2 = " Apples"

str1 + str(num_apples) + str2

'I ate 10 Apples'

 

As str() is built-in function, avoid this as an identifier in you program.
 
If you do so, you are overriding the built-in function implementation.
 

str = “sample string”

Here str is variable name, which is reference to a string object.

num_apples = 20

num_apples is variable which is a reference to an integer object.

Now if you try to use str() type constructor on num_apples
 

str(num_apples)

 
An error is thrown, as below.
 

Traceback (most recent call last):

File "", line 1, in

TypeError: 'str' object is not callable

 

As str variable overrides the str() built-in definition.

To get str() back, you have to get rid of str variable,

We can use del statement to delete the variable.
 

del str

 
With this you are deleting str variable from current namespace.

now str() type constructor works fine.
 

str(num_apples)

'20'

 

So try avoiding keyword and built-in names as the user-defined identifier.

Like, you convert numbers to strings, with str() type constructor,

You can also get a number from a string, which is number in string format.

Example.

Take two numbers form user and add them.
 

a = input("Enter a number to add :")

Enter a number to add :10

b = input("Enter a number to add :")

Enter a number to add :20

a + b

output:

'1020'

 
What happened? we did not get the expected output why?

Lets check what is the type of a and b.
 

type(a)

<class 'str'>

type(b)

<class 'str'>

a and b are string types, we got a concatenated ‘1020’ string.

To get the expected result, we have to use int() type, constructor.

a = input("Enter a number to add :")

Enter a number to add :10

b = input("Enter a number to add :")

Enter a number to add :20

int(a) + int(b)

output:

30

 

String duplication operation.

Ex 1:

 

If you want to say Happy Birthday to You, multiple times you can use ( * ) duplication operator.

wish_you = "Happy Birthday to You "

wish_you * 3

output:

'Happy Birthday to You Happy Birthday to You Happy Birthday to You '

 
Ex 2:
 


'-' * 30 # duplicates '-' 30 times

output:

'------------------------------'

 
Ex 3:
 

 "#" * 30

output:

'##############################'

 

String format method.

 
Python provides a flexible options for string formatting.

You can use % or format() function to format strings.

Example :

"Name: %s, age: %d" % ('Alister',27)

output:

'Name: Alister, age: 27'

You can also use it as

FORMAT = "Name : %s Age : %d"

print(FORMAT %('Alister',27))

output:

Name : Alister Age : 27

 

Ex 2:
 
Using format() function.
 


"Name:{}, age:{}".format("Alister",27)

"Name:{0}, age:{1}".format("Alister",27)

Above both statements gives the same result.

output:

'Name:Alister, age:27'

 

It’s possible to change the position of arguments.
 

"Name:{1}, age:{0}".format("Alister",27)

print('{}-{}-{}'.format(year, month, day))


 
format() function can also be used for text alignment.

Use

‘>’ for right alignment

‘<' for light alignment

‘^’ for center alignment

Ex 1:
 
Inserts 40 spaces before text
 

text = 'Hello, World!'

format(text, '>40')

output:


'                           Hello, World!'

 
Ex 2:
 
Inserts 40 spaces after text
 

text = 'Hello, World!'

format(text, '<40')

 
Ex 3:
 
Aligns text center with 20 spaces each side
 

text = 'Hello, World!'

format(text, '^40')


output:

'             Hello, World!              '

 
It’s possible to fill another character instead of space.

Specify the character before alignment character.

lets use “#” as filler.
 
Ex 4:
 

text = 'Hello, World!'

format(text, '#>40')

output:

'###########################Hello, World!'

 
Ex 5:
 

text = 'Hello, World!'

format(text, '#<40')

output:

'Hello, World!###########################'

 
Ex 6:
 

text = 'Hello, World!'

format(text, '#^40')



output:

'#############Hello, World!##############'


 

String Methods.

 
You can get more information about strings,

by using help() and dir() built-in functions.
 

#text is just an empty string.

text = "" 

 

 help(text)

Returns details of str object details.
 

dir(text)

 
Lists names of str object in current scope.

Now lets look at common functions of strings
 
split() #split on delimiter
 
join() #join list with delimiter
 
find() #substring search
 
strip() #remove WS
 
lower() #lowercase

upper() #uppercase
 
replace() #replace
 
ljust() #left justification
 
rjust() #right justification
 
isdigit() #check digits in string
 
 

Examples of string methods.

 

split() and join()

 
These functions are very frequently used.
 

Sytax:

S.split(sep=None, maxsplit=-1)

 

split()

 
Splits the string and returns a list of words.

It takes two optional parameters, sep and maxsplit.

sep – separator for splitting. If not specified white space is considered for split.

maxsplit – max number of splits.

 

Ex 1:
 
with default values, white space, and no maxsplit
 

 text1 = "Python is, a cool, and powerful language"

text1.split()

output:

['Python', 'is,', 'a', 'cool,', 'and', 'powerful', 'language']

 
Ex 2:

 
split by “,” as separator
 

text1.split(",")

output:

['Python is', ' a cool', ' and powerful language']

 
Ex 3:
 
split by “,” and max of 3 splits

text1.split(",", 3)

output:

['Python is', ' a cool', ' and powerful language']

 

join()

 

Opposite of split(), returns a con string from a list of words.

text1 = "Python is a cool programming language"

words = text1.split()

print(words)

output:

['Python ', 'is', 'a', 'cool', 'programming', 'language']

 

Ex 1:

joins a list of words, without any separator.

 "".join(words)

'Pythonisacoolprogramminglanguage'

 

Ex 2:
 
joins a list of words, with a space as separator.
 

 " ".join(words)

'Python is a cool programming language'

 

Ex 3:
 
joins a list of words, with a ‘,’ comma as separator.
 

 ", ".join(words)

'Python, is, a, cool, programming, language'

 

Ex 4:
 
joins a list of words, with a colon as separator.
 

":".join(words)

'Python:is:a:cool:programming:language'

 

find()

 

Search for a string, returns index of first occurrence of sub-string, else returns -1.
 
Ex 1:
 

text = "Python is a cool programming language"

print(text)

'Python is a cool programming language'

text.find('is a')

output:

7

 
Ex 2:
 

text.find('p')

output:

17

 
Ex 3:
 

text.find('P')


output:

0

 

Ex 4:
 

text.find('a')

output:

10

 

Ex 5:
 

text.find('o')

output:

4

 

text.find('not')

output:

-1

 

replace()

 
replace(old, new[, count])
 
Returns a copy of string by replacing all the old occurrences with new.
 
If count is specified, only first count occurrences are replaced.
 

text1 = "John is a good at coding, and also good at design"

 
Ex 1:
 
Replace ‘good’ with ‘best’ in text1.
 

text1.replace('good', 'best')

output:

'John is a best at coding, and also best at design'

 
Ex 2:
 

text1 = "alex, john, alex, jason, alex"

print(text1)

output:

'alex, john, alex, jason, alex'

 
Ex 3:
 
Replace all occurrences of ‘alex’ with ‘james’
 

text1.replace('alex', 'james')

output:

'james, john, james, jason, james'

 

Ex 4:
 
Replace first two occurrences of ‘alex’ with ‘james’
 

text1.replace('alex', 'james',2)

output:

'james, john, james, jason, alex'

 

Ex 5:
 
Replace first occurrences of ‘alex’ with ‘james’
 

text1.replace('alex', 'james',1)

'james, john, alex, jason, alex'

 

strip(), lstrip() rstrip()

 
strip([chars]),lstrip([chars]), rstrip([chars])

Takes optional characters, for strip.

If chars is not specified, chars defaults to white-spaces.
 

strip()

 
Returns copy of string by removing,leading and trailing white-spaces.
 

lstrip()

 
Returns copy of string by removing only leading white spaces.
 

rstrip()

 
Returns copy of string by removing only trailing white spaces.

 

text1 = " Python is a cool programming language "

print(text1)

# this contains white-spaces at both ends

ouput:

' Python is a cool programming language '

Ex 1:
 
Remove both leading and trailing white-spaces
 

text1.strip()

output:

'Python is a cool programming language'

 
Notice original string is not modified
 

print(text1)

output:

' Python is a cool programming language '

 
Ex 2:
 

Remove only leading white-spaces.

 

text1.lstrip()

output:

'Python is a cool programming language '

 
Ex 3:
 

Remove only trailing white-spaces.

 

text1.rstrip()

output:

' Python is a cool programming language'

 

Stripping other characters

 
string contains ‘#’ at start and end

 

text1 = "#####Python is a cool programming languages#####"

 
Ex 4:

Removes leading and trailing ‘#’
 

text1.strip('#')

output:

'Python is a cool programming languages'

 

Ex 5:

Removes leading ‘#’

 

text1.lstrip('#')

output:

'Python is a cool programming languages#####'

 

Ex 6:
 
Removes trailing ‘#’

 

text1.rstrip('#')

output:

'#####Python is a cool programming languages'

 

upper(), lower(), title() functions

 
As string is immutable object,
 
These functions doesn’t modify original string,
 
instead return a new string.
 

text = "Python is a cool programming language"

print(text)

output:

'Python is a cool programming language'

 

upper()

 
Returns a uppercased version of string.
 

text.upper()

'PYTHON IS A COOL PROGRAMMING LANGUAGE'

 
original string is not modified
 

print(text)

ouput:

'Python is a cool programming language'

 

lower()

 
Returns a lowercase version of string.
 

text.lower()

output:

'python is a cool programming language'

 

original string is not changed.
 

print(text)

output:

'Python is a cool programming language'

 

title()

 
Returns a title case version of string.
 

text.title()

output:

'Python Is A Cool Programming Language'

 

original string is not modified
 

print(text)

ouput:

'Python is a cool programming language'

 

isupper(), islower(), istitle()

 

isupper()

 
Returns True, if all characters in string are UPPERCASE, False otherwise.
 

islower()

 
Returns True if all the characters in a string are lowercase False otherwise.
 

istitle()

 
Returns True if all the characters in a string are Titlecase False otherwise.
 

 

text1 = "ALEX IS A SMART GUY"

text2 = "alex is a smart guy"

text3 = "Alex Is A Smart Guy"

Let's verify these strings, with below examples.


Ex 1:

text1 is in upper case

&nbsp;

text1.isupper()

output:

True

 
Ex 2:
 

text2 is in lowercase

text2.islower()

output:

True

 

Ex 3:
 

text.istitle()

output:

False

 

Ex 4:

text3 is Titlecased
 

text3.istitle()

output:

True

Ex 5:

text1 is not Titlecased
&nbsp;

text1.istitle()

output:

False

 

Ex 6:

text3 is not uppercased
 

text3.isupper()

output:

False

 

Ex 7:

text3 is not lowercased
 

text3.islower()

output:

False

 

isdigit(), isalpha(), isalnum()

isdigit()

Returns True if all characters in string are digits, False otherwise.

isalpha()

Returns True if all characters in string are alphabets.

isalnum()

Returns True if the characters in string are alphanumeric.

In below example numbers is a string, contains only digits.

Ex 1:

Check for digits

 

numbers = "12331533"

numbers.isdigit()

ouput:

True

 

Ex 2:

characters is a string, contains only alphabets.

 

characters = "abcdef"

characters.isalpha()

ouput:

True

 

Ex 3:
mixed_string is a string, contains alphanumeric characters.

 

mixed_string = "123abcd456"

mixed_string.isalnum()

ouput:

True

 

Ex 4:

mixed_string_1 is with spacial characters

 

mixed_string_1 = "@#@#[email protected]#"

mixed_string_1.isalpha()

ouput:

False

 

Ex 5:

check if mixed_string_1 contains only digits.

mixed_string_1.isdigit()

output:

False

 

Ex 6:

check if mixed_string is alphanumeric.

 

mixed_string.isdigit()

output:

False

 

Ex 7:

Another string mixed_string_2 contains special character, with alphanumeric.

 

mixed_string_2 = "abdcee123#"

mixed_string_2.isalnum()

output:

False

mixed_string_2.isdigit()

ouput:

False

 

ljust(), rjust(), center()

 
ljust(width[, fillchar])
 
Returns a left justified string of length width. Padding is done with fillchar.

If ‘fillchar’ is not specified white-space is used.
 

Ex 1:
 
Left justify string length 40 with white-spaces.
 

title = "Just Learn Python"

len(title)

output:

17


#Left justify with string length 40

title.ljust(40)

output:
'Just Learn Python                       '


#note length of original string title is not changed

len(title)

output:

17

#length of new string returned by title.ljust(40)

len(title.ljust(40))

output:

40

 

Ex 2:
 
Left justify with string length 40, pad with “#”

 

title.ljust(40, "#")

output:

'Just Learn Python#######################'


 

Ex 3:
 
Right justify with string length 40
 

title.rjust(40)

output:

'                       Just Learn Python'

 
Ex 4:
 
Right justify with string length 40, pad with “#”

 

title.rjust(40, "#")

'#######################Just Learn Python'


 

Ex 5:
 
Align center with string length 40

 

title.center(40)

output:

'           Just Learn Python            '

 
Ex 6:
 

Align center with string length 40, pads with “#”

 

title.center(40, "#")

'###########Just Learn Python############'


 

s.endswith()

 

S.endswith(suffix[, start[, end]])
 
Return True if S ends with the specified suffix, False otherwise.

check from start to end position, if specified, otherwise check in
at the end of string.
 
Validate ends with period
 

Ex 1:

 

title = "Just Learn Python."

title.endswith('.')

output:

True

 

repace()

 
s.replace(old,new[,count])

Replace old with new, for first count occurrences.
 

Ex 1:
 
Replace ‘.’ with a ‘?’

 

title = "Just Learn Python."

title.replace('.','?')

output:

'Just Learn Python?'

 

That’s covers the basics of strings in Python, you can also check out Exercises on strings.