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

Basic Strings

The document demonstrates various string methods in Python like indexing, slicing, counting characters, checking start/end, splitting, uppercase/lowercase conversion. It contains code snippets that create a sample string and apply different string methods on it to extract information. The output from each code snippet is displayed to show the results of the string methods.

Uploaded by

Riya Ram
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Basic Strings

The document demonstrates various string methods in Python like indexing, slicing, counting characters, checking start/end, splitting, uppercase/lowercase conversion. It contains code snippets that create a sample string and apply different string methods on it to extract information. The output from each code snippet is displayed to show the results of the string methods.

Uploaded by

Riya Ram
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. astring = "Hello world!

"

print("single quotes are ' '")

print(len(astring))

O/P

single quotes are ' ' 12

2. astring = "Hello world!"

sprint(astring.index("o")) OP 4

3. astring = "Hello world!"


print(astring.count("l")) OP 3
4. astring = "Hello world!"
print(astring[3:7]) OP lo w
5. astring = "Hello world!"
print(astring[3:7:2]) l
This prints the characters of string from 3 to 7 skipping one character. This is extended slice
syntax. The general form is [start:stop:step].

6. astring = "Hello world!"


print(astring[::-1]) !dlrow olleH
There is no function like strrev in C to reverse a string. But with the above mentioned type of
slice syntax you can easily reverse a string like this
7. astring = "Hello world!"
print(astring.upper())
print(astring.lower()) HELLO WORLD!
hello world!
8. astring = "Hello world!"
print(astring.startswith("Hello"))
print(astring.endswith("asdfasdfasdf"))
OUT True
False
9. astring = "Hello world!"
afewwords = astring.split(" ")
astring = "Hello world!"

afewwords = astring.split("l")
print(afewwords)
out
['He', '', 'o wor', 'd!']
CODE

s = "Strings are awesome!"


# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8


print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2


print("a occurs %d times" % s.count("a"))

# Slicing the string into bits


print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase


print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase


print("String in lowercase: %s" % s.lower())

# Check how a string starts


if s.startswith("Str"):
print("String starts with 'Str'. Good!")

# Check how a string ends


if s.endswith("ome!"):
print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,


# each containing only a word
print("Split the words of the string: %s" % s.split(" "))

<script.py> output:
Length of s = 20
The first occurrence of the letter a = 8
a occurs 2 times
The first five characters are 'Strin'
The next five characters are 'gs ar'
The thirteenth character is 'a'
The characters with odd index are 'tig r wsm!'
The last five characters are 'some!'
String in uppercase: STRINGS ARE AWESOME!
String in lowercase: strings are awesome!
String starts with 'Str'. Good!
String ends with 'ome!'. Good!
Split the words of the string: ['Strings', 'are', 'awesome!']

In [1]:

CODE 1
s = "Hey there! what should this string be?"
# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8


print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2


print("a occurs %d times" % s.count("a"))

# Slicing the string into bits


print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase


print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase


print("String in lowercase: %s" % s.lower())

# Check how a string starts


if s.startswith("Str"):
print("String starts with 'Str'. Good!")

# Check how a string ends


if s.endswith("ome!"):
print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,


# each containing only a word
print("Split the words of the string: %s" % s.split(" "))

OUTPUT

<script.py> output:
Length of s = 38
The first occurrence of the letter a = 13
a occurs 1 times
The first five characters are 'Hey t'
The next five characters are 'here!'
The thirteenth character is 'h'
The characters with odd index are 'e hr!wa hudti tigb?'
The last five characters are 'g be?'
String in uppercase: HEY THERE! WHAT SHOULD THIS STRING BE?
String in lowercase: hey there! what should this string be?
Split the words of the string: ['Hey', 'there!', 'what', 'should', 'this', 'string', 'be?']

You might also like