String

1.1 in

inworks somewhat differently with strings. It evaluates to Trueif one string is a substring of another:

>>> 'p' in 'apple'
True
>>> 'i' in 'apple'
False
>>> 'ap' in 'apple'
True
>>> 'pa' in 'apple'
False

Note that a string is a substring of itself, and the empty string is a substring of any other string. (Also note that computer programmers like to think about these edge cases quite carefully!)

>>> 'a' in 'a'
True
>>> 'apple' in 'apple'
True
>>> '' in 'a'
True
>>> '' in 'apple'
True

1.2 String join()

ref: https://www.programiz.com/python-programming/methods/string/join

Syntax: string.join(iterable)

Some of the example of iterables are:

  • Native datatypes : List, Tuple, String, Dictionary, Set
  • File objects and objects you define with an __iter__() or __getitem()__ method

The join() method returns a string concatenated with the elements of an iterable.

If the iterable contains any non-string values, it raises a TypeError exception.

numList = ['1', '2', '3', '4']
seperator = ', '
print(seperator.join(numList))

numTuple = ('1', '2', '3', '4')
print(seperator.join(numTuple))

s1 = 'abc'
s2 = '123'

""" Each character of s2 is concatenated to the front of s1""" 
print('s1.join(s2):', s1.join(s2))

""" Each character of s1 is concatenated to the front of s2""" 
print('s2.join(s1):', s2.join(s1))

## Output
1, 2, 3, 4
1, 2, 3, 4
s1.join(s2): 1abc2abc3
s2.join(s1): a123b123c

results matching ""

    No results matching ""