1 of 23

Python�String

2 of 23

What is String ?�

  • A string is a sequence of characters used to represent text in programming. It can include letters, numbers, symbols, and spaces. which is enclosed by single, double and triple quotes (‘ ‘, “ “, ‘’’ ‘’’)
  • For example, "Hello" and "123"

3 of 23

Introducing CRUD Operation

4 of 23

How to Create String?

  • We can declare passing n number of characters within single quotes or double quotes .
  • For ex : x = “Reviving India”

5 of 23

Ways to Read Data from string

6 of 23

Indexing in Python

  • It refers to the position of an element in a string . It can be positive indexing & negative indexing.
  • Positive Index start with 0 ,1,2 .. and so on and 0 denote the first element
  • Negative Index start with -1,-2,-3 and so on and -1 denote the last element

7 of 23

Slicing in Python

  • It used to access multiple elements from the string.
  • Syntax : [start : stop : step]

8 of 23

How to Update String

  • Strings are immutable in Python, so you can't change a string directly.
  • Instead, create a new string with the desired changes.

9 of 23

How to Delete String?

  • We can’t delete any chracters from the string since the string is a immutable datatype.
  • But we can delete entire data from the variable.

10 of 23

String Methods

  • To manipulate strings , use builtin string methods..

  • All string methods returns new values. They do not change the original string.

11 of 23

capitalize()

  • string.capitalize()

  • The capitalize() method returns a string where the first character is upper case.

  • No parameters�

12 of 23

Casefold()

  • The casefold() method returns a string where all the characters are lower case.

  • string.casefold()

  • No parameters

13 of 23

count

  • The count() method returns the number of times a specified value appears in the string.
  • string.count(value, start, end)

value

Required. A String. The string to value to search for

start

Optional. An Integer. The position to start the search. Default is 0

end

Optional. An Integer. The position to end the search. Default is the end of the string

14 of 23

find

  • The find() method finds the first occurrence of the specified value.
  • The find() method returns -1 if the value is not found.

  • string.find(value, start, end)

15 of 23

index

  • The index() method finds the first occurrence of the specified value.

  • the only difference is that the index() method raises an exception if the value is not found.

16 of 23

join

  • The join() method takes all items in an iterable and joins them into one string.

  • string.join(iterable)

iterable

Required. Any iterable object where all the returned values are strings

17 of 23

lower

  • The lower() method returns a string where all characters are lower case.

  • string.lower()

  • No parameters

18 of 23

replace

  • The replace() method replaces a specified phrase with another specified phrase. (All Occurrence)

  • string.replace(oldvalue, newvalue, count)

oldvalue

Required. The string to search for

newvalue

Required. The string to replace the old value with

count

Optional. A number specifying how many occurrences of the old value you want to replace. Default is all occurrences

19 of 23

split

  • The split() method splits a string into a list.

  • You can specify the separator, default separator is any whitespace.

  • string.split(separator, maxsplit)

20 of 23

swapcase

  • The swapcase() method returns a string where all upper case letters are lower case and vice versa.

  • string.swapcase()

  • No parameters.

21 of 23

title

  • The title() method returns a string where the first character in every word is upper case.

  • string.title()

  • No parameters.

22 of 23

upper

  • The upper() method returns a string where all characters are in upper case.

  • string.upper()

  • No parameters

23 of 23

len

  • returns the length of the string.

  • len( str )

  • No Parameters.