Skip to main content

STRINGS

Title

title() displays each word in titlecase, where each word begins with a capital letter.

name = "ram mohan"
print(name.title()); #output >>> Ram Mohan

Upper & Lower

name2 = "Sam Prabu"
print(name2.upper()) #output >>> SAM PRABU
print(name2.lower()) #output >>> sam prabu

Concatenating Strings

Concatenating strings means combining strings.

Example: To store a surname name and a name in separate variables, and then to combine them.

surname = 'Thota'
name = 'Ramudu'

full_name = surname + ' ' + name
print(full_name) #output >>> Thota Ramudu

Tabs or Newlines

For easy reading of the output we can use Tabs(\t) or newlines (\n)

surname = 'Thota'
name = 'Ramudu'

full_name = surname + ' ' + name
print(surname+'\n'+name + '\n\t' + full_name)

#output
Thota
Ramudu
Thota Ramudu

Stripping Whitespace

  1. rstrip(): strip whitespace from the right side
  2. lstrip(): strip whitespace from the left side
  3. strip(): strip whitespace from both sides

Example:

country = ' India '
# strip whitespace from the right side
print(country.rstrip())
# strip whitespace from the left side
print(country.lstrip())
# strip whitespace from both sides
print(country.strip())

#Output
India
India
India

str conversion

str(): str() function tells Python to represent non-string values as strings.

Example

age = 43
message = "Happy " + str(age) + "rd Birthday!"
print(message)

#output
Happy 43rd Birthday!

Numbers

Integers

>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6

Float

>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.2
0.4
>>> 2 * 0.1
0.2