0% found this document useful (0 votes)
19 views

Python Strings

notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Python Strings

notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to Python string

A string is a series of characters. In Python, anything inside quotes is a string.


And you can use either single or double quotes.

Example 1:

str1="This is a String"

str2='''This is a String'''

str3='This is a String'

print(str1)

print(str2)

print(str3)

Output:

This is a String

This is a String

This is a String

Example 2:

str1="This is a "important" String"

print(str1)

Output:

Error

Example 3:

str1='This is a "important" String'

print(str1)

Output:

This is a "important" String

Example 4:
str1='This is Kapil's notebook'

print(str1)

Output:

Error

Example 5:

str1="This is Kapil's notebook"

print(str1)

Output:

This is Kapil's notebook

Example 6:

print('''this is

a string

in seperate

lines''')

Output:

this is

a string

in seperate

lines

Example 7:

str='it's a wonderful topic'

print(str)

Output:

Error

Example 8:
str='it\'s a wonderful topic'

print(str)

Output:

it's a wonderful topic

Note: We can use value of one string variable into another.

Example:

name = 'John'

message = 'Hi {name}'

print(message)

Output:

Hi {name}

Concatenating Python strings

Concatenation of string means to join (or append) one string after another. For
concatenation of string we need to define two strings and use + operator.

Example:

msg1 = 'Good'

msg2 = ' Morning'

msg3=msg1+msg2

print(msg3)

Output:

Good Morning

String Functions

Count:

Return the number of times the value "apple" appears in the string.
Example:

txt="I am learning string functions. It's fun to learn about string"

n=txt.count("string")

print(n)

Output:

Find:

Searches the string for a specified value and returns the position of where it
was found

Example 1:

txt="I am learning string functions. It's fun to learn about string"

n=txt.find("string")

print(n)

Output:14 # string mentioned starts from

Example 2: txt="I am learning string functions. It's fun to learn about string"

n=txt.find("xyz")

print(n)

Output: -1 #as the string mentioned does not exist

Capitalize:

The capitalize() method returns a string where the first character is upper case,
and the rest is lower case.

Example:

txt="i am learning string functions. It's fun to learn about string"

n=txt.capitalize()

print(n)

Output:

I am learning string functions. It's fun to learn about string


Title:

Converts the first character of each word to upper case

Example:

string functions. It's fun to learn about string"

n=txt.title()

print(n) txt="i am learning

Output:

I Am Learning String Functions. It'S Fun To Learn About String

Lower:

Converts a string into lower case

Example:

txt="I Am Learning String Functions. It'S Fun To Learn About String"

n=txt.lower()

print(n)

Output:

i am learning string functions. it's fun to learn about string

upper:

Converts a string into upper case

Example:

txt="I Am Learning String Functions. It'S Fun To Learn About String"

n=txt.upper()

print(n)

Output:

I AM LEARNING STRING FUNCTIONS. IT'S FUN TO LEARN ABOUT STRING

Swapcase:

Swaps cases, lower case becomes upper case and vice versa
Example:

txt="i AM LEARNING string FUNCTIONS. It'S fun TO LEARN ABOUT string"

n=txt.swapcase()

print(n)

Output:

I am learning STRING functions. iT's FUN to learn about STRING

Replace:

Returns a string where a specified value is replaced with a specified value

Example:

txt="I am learning string functions. It's fun to learn about string"

n=txt.replace("string","python")

print(n)

Output:

I am learning python functions. It's fun to learn about python

Join:

Converts the elements of an iterable into a string. In simpler words, it join all
items in a tuple into a string, using a hash character as separator

Example:

txt=("unit 1", "unit 2", "unit 3","unit 4")

n=".".join(txt)

print(n)

Output:

unit 1.unit 2.unit 3.unit 4

isspace:

Returns True if all characters in the string are whitespaces.


The isspace() method returns True if all the characters in a string are
whitespaces, otherwise False.
Example 1:

txt=" "

n=txt.isspace()

print(n)

Output:

TURE

Example 2:

##txt="I am learning string functions. It's fun to learn about string"

txt=" "

n=txt.isspace()

print(n)

Output:

FALSE

Isdigit:

Returns True if all characters in the string are digits. The isdigit() method
returns True if all the characters are digits, otherwise False.

Example 1:

txt="12345"

n=txt.isdigit()

print(n)

Output:

TRUE

Example 2:

txt="12345A"

n=txt.isdigit()

print(n)

Output:
FALSE

isaplha()

Returns True if all characters in the string are alphabets. The isaplha() method
returns True if all the characters are alphabets, otherwise False.

Example:

str1="ThisIsGood"

print(str1.isalpha())

Output:

True

Slicing: will find substring from a string

Example:

a = ("a", "b", "c", "d", "e", "f", "g", "h")

x = slice(3, 5)

print(a[x])

Output:

('d', 'e')

Example 2:

s=’ HELLO ‘

print(s[3:6])

Output:

HEL

Split:

Splits the string at the specified separator, and returns a list. You can specify
the separator, default separator is white space.

Example:

txt="I am learning string functions. It's fun to learn about string"


n=txt.split()

print(n)

Output:

['I', 'am', 'learning', 'string', 'functions.', "It's", 'fun', 'to', 'learn', 'about', 'string']

Startswith:

Returns true if the string starts with the specified value.


The startswith() method returns True if the string starts with the specified
value, otherwise False.

Example 1:

txt="I am learning string functions. It's fun to learn about string"

n=txt.startswith("learning")

print(n)

Output:

False

Example 2:

txt="I am learning string functions. It's fun to learn about string"

n=txt.startswith("I")

print(n)

Output:

TRUE

Endwith()

Returns true if the string ends with the specified value

Example 1:

txt="I am learning string functions. It's fun to learn about string"

n=txt.endswith("string")

print(n)

Output:
TRUE

Example 2:

txt="I am learning string functions. It's fun to learn about string"

n=txt.endswith("str")

print(n)

Output:

FALSE

You might also like