0% found this document useful (0 votes)
24 views1 page

Strings

The document provides an overview of strings in programming, explaining their definition, indexing, slicing, and various methods/functions available for manipulation. It includes examples of string operations such as upper/lower case conversion, checking start/end characters, splitting, counting occurrences, and replacing substrings. Additionally, it covers string formatting and joining techniques, demonstrating how to work with strings effectively in programming.

Uploaded by

Satya Kumar
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)
24 views1 page

Strings

The document provides an overview of strings in programming, explaining their definition, indexing, slicing, and various methods/functions available for manipulation. It includes examples of string operations such as upper/lower case conversion, checking start/end characters, splitting, counting occurrences, and replacing substrings. Additionally, it covers string formatting and joining techniques, demonstrating how to work with strings effectively in programming.

Uploaded by

Satya Kumar
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/ 1

strings

1. String is a sequence of characters enclosed with ' ' or " " 2. String items can be accessed by referring to their index values 3. string can be treated as an array Array is a collection of data items which are of same data type
In [1]: a = 'python'

In [2]: type(a)

Out[2]: str

In [3]: a = 'programming'

In [4]: #string indexing - It is a process of accessing string characters by


#referring to their index values (positive index / negative index)

In [5]: a[0]

Out[5]: 'p'

In [6]: a[1]

Out[6]: 'r'

In [7]: a[2]

Out[7]: 'o'

In [8]: a

Out[8]: 'programming'

In [9]: a[-1]

Out[9]: 'g'

In [10]: a[-2]

Out[10]: 'n'

In [11]: a[-3]

Out[11]: 'i'

string slicing
In [12]: #accessing a part of string by referring to their index values

In [13]: a = 'programming'

In [14]: a[:] # all characters of astring

Out[14]: 'programming'

In [15]: a[::] # all characters of a string

Out[15]: 'programming'

In [16]: a[::-1] # all characters of a string in a reverse order

Out[16]: 'gnimmargorp'

In [17]: a[0:5] # end index is not included

Out[17]: 'progr'

In [18]: a

Out[18]: 'programming'

In [19]: a[2:7]

Out[19]: 'ogram'

In [21]: a[-8:-2] # slicing with negative index

Out[21]: 'grammi'

In [22]: a[:6] # implied zero # a[0:6]

Out[22]: 'progra'

In [23]: a[4:] #implied end #a[4:end of the string]

Out[23]: 'ramming'

In [24]: # slicing with step size


a[0:8:2] # skipping one char a[SI:EI:stepsize]

Out[24]: 'porm'

In [25]: a[0:8:3]

Out[25]: 'pgm'

In [26]: a[-10:-2]

Out[26]: 'rogrammi'

In [27]: a[-11:-2:2]

Out[27]: 'pormi'

string Methods/functions
In [28]: a = 'programming'

In [29]: a

Out[29]: 'programming'

In [31]: a.upper() # upper() - converts lower case chars to upper case

Out[31]: 'PROGRAMMING'

In [32]: b = 'ProGraMMinG'
b.upper()

Out[32]: 'PROGRAMMING'

In [33]: a

Out[33]: 'programming'

In [34]: b

Out[34]: 'ProGraMMinG'

In [35]: c = b.upper()

In [36]: c

Out[36]: 'PROGRAMMING'

In [37]: c

Out[37]: 'PROGRAMMING'

In [38]: c.lower() # lower() - converts all upper case characters to lower case

Out[38]: 'programming'

In [39]: #startswith() - check if a string starts with a specified char/sequence


#of characters

In [40]: a

Out[40]: 'programming'

In [42]: a.startswith('p')

Out[42]: True

In [43]: a.startswith('pr')

Out[43]: True

In [44]: a = 'python is good programming language'

In [45]: a.startswith('python')

Out[45]: True

In [46]: a.startswith('java')

Out[46]: False

In [47]: a.startswith('P')

Out[47]: False

In [48]: #endswith() - checks if a string ends with a specified char/sequence of


#characters

In [49]: a

Out[49]: 'python is good programming language'

In [50]: a.endswith('e')

Out[50]: True

In [51]: a.endswith('ge')

Out[51]: True

In [52]: a.endswith('language')

Out[52]: True

In [53]: a.endswith('satya')

Out[53]: False

In [54]: #split() - splits a string based on a separator and returns list of


#strings

In [55]: a = 'python programming is good programming language'

In [56]: a.split() # space is the default separator

Out[56]: ['python', 'programming', 'is', 'good', 'programming', 'language']

In [57]: b = 'python/good/program'
b.split('/')

Out[57]: ['python', 'good', 'program']

In [58]: a.split('p')

Out[58]: ['', 'ython ', 'rogramming is good ', 'rogramming language']

In [59]: a

Out[59]: 'python programming is good programming language'

In [60]: b = 'programming'
b.split('g')

Out[60]: ['pro', 'rammin', '']

In [61]: b.split('m')

Out[61]: ['progra', '', 'ing']

In [62]: b

Out[62]: 'programming'

In [63]: #index() - returns index of first occurance of specified char/seq


#of chars

In [64]: a

Out[64]: 'python programming is good programming language'

In [65]: b

Out[65]: 'programming'

In [66]: b.index('i')

Out[66]: 8

In [67]: b.index('p')

Out[67]: 0

In [68]: b.index('m')

Out[68]: 6

In [69]: b.rindex('m')

Out[69]: 7

In [1]: #find() - returns index of first occurance of specified char / sequence of char

In [2]: a = 'python programming'

In [3]: a

Out[3]: 'python programming'

In [4]: a.find('p')

Out[4]: 0

In [5]: a.find('m')

Out[5]: 13

In [6]: a.find('o')

Out[6]: 4

In [7]: a.rfind('m')

Out[7]: 14

In [8]: a.rfind('o')

Out[8]: 9

In [10]: a

Out[10]: 'python programming'

In [11]: a.index('x')

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[11], line 1
----> 1 a.index('x')

ValueError: substring not found

In [12]: a.find('x')

Out[12]: -1

In [13]: #input() - it takes dynamic inputs from the user

In [16]: a = int(input('Enter a value: '))


b = int(input('Enter b value: '))
c = a+b
print('The result is : ',c)

Enter a value: 67
Enter b value: 90
The result is : 157

In [17]: a = input('Enter your string:')


print(a.index('z'))
print('Execution done')
print('Example for index method')

Enter your string:python programming


---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[17], line 2
1 a = input('Enter your string:')
----> 2 print(a.index('z'))
3 print('Execution done')
4 print('Example for index method')

ValueError: substring not found

In [18]: a = input('Enter your string:')


print(a.find('z'))
print('Execution done')
print('Example for find method')

Enter your string:python programming


-1
Execution done
Example for find method

In [19]: a

Out[19]: 'python programming'

In [20]: #count()- it returns number of occurances of a spcified character

In [21]: a

Out[21]: 'python programming'

In [22]: a.count('p')

Out[22]: 2

In [23]: a.count('m')

Out[23]: 2

In [24]: a.count('o')

Out[24]: 2

In [25]: a.count('y')

Out[25]: 1

In [26]: #replace() - it is used to replace a specified char/sequence of


#characters

In [27]: a

Out[27]: 'python programming'

In [28]: a.replace('python','java')

Out[28]: 'java programming'

In [29]: b = 'python'
b.replace('p','j')

Out[29]: 'jython'

In [30]: a

Out[30]: 'python programming'

In [31]: a.replace('p','j')

Out[31]: 'jython jrogramming'

In [32]: a.replace('p','j',1)

Out[32]: 'jython programming'

In [35]: a = 'python programming is good programming. python is easy to learn'

In [36]: a.replace('python','java',1)

Out[36]: 'java programming is good programming. python is easy to learn'

In [37]: a.replace('python','java',2)

Out[37]: 'java programming is good programming. java is easy to learn'

In [38]: a = 'python'

In [39]: #center() - centers a string

In [40]: a = 'python'
a.center(10)

Out[40]: ' python '

In [41]: a = 'kumar'
a.center(10)

Out[41]: ' kumar '

In [42]: a = 'pythons'

In [46]: a.center(12)

Out[46]: ' pythons '

In [54]: a = 'python'
a.center(13)

Out[54]: ' python '

In [55]: #capitalize() - it capitalizes first character of a string

In [56]: a = 'programming'

In [57]: a.capitalize()

Out[57]: 'Programming'

In [58]: a = 'python is good programming language'


a.capitalize()

Out[58]: 'Python is good programming language'

In [59]: #title() - it capitalizes first character of each word in a string

In [60]: a = 'python programming is good programming language'

In [61]: a.title()

Out[61]: 'Python Programming Is Good Programming Language'

In [62]: a = 'innomatics research labs'


a.title()

Out[62]: 'Innomatics Research Labs'

In [63]: #strip() - strips spaces

In [64]: a = ' programming'

In [65]: a

Out[65]: ' programming'

In [66]: a.strip()

Out[66]: 'programming'

In [67]: a = ' Programming '

In [68]: a

Out[68]: ' Programming '

In [69]: a.strip()

Out[69]: 'Programming'

In [70]: a = ' python programming is good. '


a.strip()

Out[70]: 'python programming is good.'

In [71]: a = ' python '


a.lstrip()

Out[71]: 'python '

In [72]: a = 'python. '


a.rstrip()

Out[72]: 'python.'

In [73]: a = 'python'
a.isupper()

Out[73]: False

In [74]: a = 'PYTHON'
a.isupper()#returns true if all characters are upper case

Out[74]: True

In [75]: a = 'python'
a.islower() #returns true if all characters are lower case

Out[75]: True

In [76]: a.isalpha() # returns true if characters are alphabets

Out[76]: True

In [77]: a ='python6'
a.isalpha()

Out[77]: False

In [78]: a = '12345'

In [79]: a

Out[79]: '12345'

In [80]: a.isalnum() # returns true if all characters are alphanumeric in a string

Out[80]: True

In [81]: a = 'python456'
a.isalnum()

Out[81]: True

In [82]: a = '''university of hyderabad was established under act of universities


development act. under this act many universities were established and
cerntral universities in india perfoming far beyond expectation'''

In [83]: #1. count the number of occurances of a word under


#2. count the number of occurances of a word universities
#3. count the number of words in the given string
#4. count the number of occurances of a character 'o'

In [84]: a

Out[84]: 'university of hyderabad was established under act of universities\ndevelopment act. under this act many universities were established and \ncerntral universities in india perfoming far beyond expectation'

In [85]: b = a.split()

In [86]: b

Out[86]: ['university',
'of',
'hyderabad',
'was',
'established',
'under',
'act',
'of',
'universities',
'development',
'act.',
'under',
'this',
'act',
'many',
'universities',
'were',
'established',
'and',
'cerntral',
'universities',
'in',
'india',
'perfoming',
'far',
'beyond',
'expectation']

In [87]: len(b) # total number of words

Out[87]: 27

In [88]: len(a) # total number of characters

Out[88]: 201

In [89]: a.count('o') # total number of 'o'

Out[89]: 6

In [90]: #join() - joins all strings in an iterable with separator

In [91]: T = ('satya','kumar','raj','arun')

In [92]: ' '.join(T)

Out[92]: 'satya kumar raj arun'

In [93]: '#'.join(T)

Out[93]: 'satya#kumar#raj#arun'

In [94]: T

Out[94]: ('satya', 'kumar', 'raj', 'arun')

In [95]: for i in T:
print(i)

satya
kumar
raj
arun

In [96]: '%'.join(T)

Out[96]: 'satya%kumar%raj%arun'

In [97]: #format() - formats specified values and insert them in a string


#placeholder. string placeholder is defined using {}

In [98]: txt = 'My name is {fname}, my age is {age}'.format(fname='satya',age =32)

In [100… print(txt)

My name is satya, my age is 32

In [101… S = 'My name is {fname},my age is{age}'

In [102… S.format(fname='satya',age=32)

Out[102]: 'My name is satya,my age is32'

In [ ]:

You might also like