Len s: Python len() Function

Python len()

The len() function returns the number of items (length) in an object.

Example

languages = ['Python', 'Java', 'JavaScript']

# compute the length of languages length = len(languages)

print(length) # Output: 3

len() Syntax

The syntax of len() is:

len(s)

len() Parameters

The len() function takes a single argument s, which can be

  • sequence – string, bytes, tuple, list, range OR,
  • collection – dictionary, set, frozen set

len() Return Value

len() function returns the number of items of an object.

Failing to pass an argument or passing an invalid argument will raise a TypeError exception.


Example 1: How len() works with tuples, lists and range?

testList = []

print(testList, 'length is', len(testList))

testList = [1, 2, 3] print(testList, 'length is', len(testList)) testTuple = (1, 2, 3)

print(testTuple, 'length is', len(testTuple))

testRange = range(1, 10) print('Length of', testRange, 'is', len(testRange))

Output

[] length is 0
[1, 2, 3] length is 3
(1, 2, 3) length is 3
Length of range(1, 10) is 9

Visit these pages to learn more about:

  • Python Lists
  • Python Tuples
  • Python range() Method

Example 2: How len() works with strings and bytes?

testString = ''
print('Length of', testString, 'is', len(testString))
testString = 'Python'

print('Length of', testString, 'is', len(testString))

# byte object testByte = b'Python' print('Length of', testByte, 'is', len(testByte)) testList = [1, 2, 3] # converting to bytes object testByte = bytes(testList)

print('Length of', testByte, 'is', len(testByte))

Output

Length of  is 0
Length of Python is 6
Length of b'Python' is 6
Length of b'\x01\x02\x03' is 3

Visit these pages to learn more about:

  • Python Strings
  • Python bytes() Method

Example 3: How len() works with dictionaries and sets?

testSet = {1, 2, 3}

print(testSet, 'length is', len(testSet))

# Empty Set testSet = set() print(testSet, 'length is', len(testSet)) testDict = {1: 'one', 2: 'two'}

print(testDict, 'length is', len(testDict))

testDict = {} print(testDict, 'length is', len(testDict)) testSet = {1, 2} # frozenSet frozenTestSet = frozenset(testSet)

print(frozenTestSet, 'length is', len(frozenTestSet))

Output

{1, 2, 3} length is 3
set() length is 0
{1: 'one', 2: 'two'} length is 2
{} length is 0
frozenset({1, 2}) length is 2

Visit these pages to learn more about:

  • Python Dictionary
  • Python Set
  • Python frozenset()

Internally, len() calls the object’s __len__ method. You can think of len() as:

def len(s):
    return s.__len__()

So, you can assign custom length to the object (if necessary)


Example 4: How len() works for custom objects?

class Session:
    def __init__(self, number = 0):
      self.number = number
    
    def __len__(self):
      return self.number
# default length is 0
s1 = Session()

print(len(s1))

# giving custom length s2 = Session(6)

print(len(s2))

Output

0
6

How to find length of the string

BySteve Campbell

HoursUpdated

Python len() Function

len() is a built-in function in python. You can use the len() to get the length of the given string, array, list, tuple, dictionary, etc. You can use len function to optimize the performance of the program. The number of elements stored in the object is never calculated, so len helps provide the number of elements.

Syntax

len(value)

Parameters

Value: the given value you want the length of.

Return value

It will return an integer value i.e. the length of the given string, or array, or list or collections.

Various types of Return values:

Strings:

It returns the number of characters in a string, which includes punctuation, space, and all type of special characters. However, you should be very careful while using len of a Null variable.

Empty:

Empty is a second return call which has zero characters, but it is always None.

Collections:

The len built-in returns the number of elements in a collection.

TypeError:

Len function depends on the type of the variable passed to it. A Non-Type does not have any built-in support.

Dictionary:

For the dictionary, each pair is counted as one unit. However, values and keys are not independent.

Example 1: How find the length of the given string?

# testing len() 
str1 = "Welcome to  Guru99 Python Tutorials"
print("The length of the string  is :", len(str1))

Output:

The length of the string  is : 35

Example 2: How to find the length of the list in python?

# to find the length of the list
list1 = ["Tim","Charlie","Tiffany","Robert"]
print("The length of the list is", len(list1))

Output:

The length of the list is 4

Example 3: How to find the length of a tuple in python

# to find the length of the tuple
Tup = ('Jan','feb','march')
print("The length of the tuple is", len(Tup))

Output:

The length of the tuple is 3

Example 4: How to find the length of the dictionary in Python?

# to find the length of the Dictionary
Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25}
print("The length of the Dictionary is", len(Dict))

Output:

The length of the Dictionary is 4

Example 5: How to find the length of the array in python

# to find the length of the array
arr1 = ['Tim','Charlie','Tiffany','Robert']
print("The length of the Array is", len(arr1))

Output:

The length of the Array is 4

Summary

  • len() is a built-in function in python. You can use the len() to get the length of the given string, array, list, tuple, dictionary, etc.
  • Value: the given value you want the length of.
  • Return value a return an integer value i.e. the length of the given string, or array, or list, or collections.

This code is editable. Click Run to Execute

Linen with viscose fabric: description, properties, advantages

Fabric reference bookArticles

Blended fabrics are a storehouse of new properties that a fabric acquires due to a well-chosen ratio of two or more components. One of the most successful mixed solutions is linen fabric supplemented with viscose. Many brands use this combination in their products, observing different proportions of the presence of viscose and linen in fabrics.

See also:

Alpolux — insulation made of polyester microfibers and natural wool

Description and composition of the fabric

Viscose linen is a fabric in which many properties of natural linen and some parameters of viscose are corrected. In its structure, it is softer, the rigidity of flax is lost. Thanks to viscose, the fabric flows beautifully, forming soft folds and draperies. It lacks the angular clumsiness of linen. It perfectly passes air, quickly absorbs moisture. The addition of viscose as an additional additive makes it possible to obtain the following properties:

  • softness;
  • elasticity;
  • low specific gravity;
  • aesthetic appearance;
  • easy maintenance and operation;
  • good dimensional stability, low wrinkling;
  • long service life and other parameters.

Linen in combination with viscose is less whimsical in care. The combined material is easier to iron, wrinkle and shrink less when washed, while remaining strong and durable.

See also:

Natural silk is an exquisite, expensive fabric. Its properties, composition and application

The percentage of viscose and linen

Viscose linen is rightly called a new generation of fabric. It combines all the advantages of natural fabrics and the softness of viscose. Such fabrics are produced by many brands around the world. The percentage of flax and viscose fibers in them varies quite a bit. There are options in which a 50/50 percent ratio is used. There are canvases with a share of artificial raw materials from 70 to 85 percent. The more viscose, the softer the fabric. It stretches, but retains its shape with gentle care. Flax, in turn, improves the strength, air conductivity of viscose and gives it some stiffness. The properties of viscose linen directly depend on its composition and processing.

See also:

The best fabrics for tailoring underwear

Properties of viscose linen

The combined material in any case has the advantages and disadvantages of the two types of raw materials of which it is composed. Benefits include:

  • easy maintenance;
  • wrinkle resistance;
  • dimensional stability;
  • low cost;
  • strength, wear resistance, durability;
  • moisture resistance, heat and air conduction;
  • the ability to resist mechanical stress.

Fabric imperfections depend on the percentage of linen and viscose. There may be stiffness, inelasticity, flaking when cutting, a tendency to wrinkle and pilling, shrinkage, deformation when wet and intolerance to ultraviolet light. But when choosing the percentage of the mixture, they try to level these manifestations.

See also:

Cotton – cotton fabric: features and applications

Use and care of viscose linen

Mixing raw linen with viscose fiber gives natural fabrics better performance properties. Therefore, the scope of the combined material is quite large. Everyday clothes for children and adults are sewn from it: panamas, breeches, trousers, suits, dresses, skirts, t-shirts, blouses, shirts. The material is used for sewing home clothes, underwear and bed linen, sleepwear, home textiles, sometimes it is used as a lining material. Care for linen blends is no different from how to wash viscose, as it is the softer and more pliable material in this duet. A product made from it lasts a long time if you follow simple care rules:

  1. water heating up to 40°C;
  2. hand and machine washable;
  3. use standard detergents;
  4. lay flat to dry in the shade;
  5. Iron as needed with a damp protective cloth.

According to the reviews of many users of this fabric, blended linen is a more comfortable and practical fabric, suitable for sewing things for the summer season.

Get access to your personal account
wholesale customer

Access

Herringbone linen. Plain fabrics

CODE: 14-YL301

3840 ₽ / m.p.

Art. No. YL301
Collection 14 – Plain fabrics
Fabric weight 380 g/m 2
Width 280 cm
Composition 25% linen; 75% polyester
Print type Plain
Available from stock

1

61. 6 m

2

48 m

3

52 m

4

28.2 m

5

20 m

6

72.2 m

7

41.5 m

8

26 m

9

21 m

10

7.5 m

30 m

11

16.1 m

12

44.5 m

13

12.7 m

30 m

14

37 m

15

17.3 m

16

21.4 m

17

19 m

18

36.5 m

19

12.5 m

30 m

20

30.5 m

21

24.8 m

22

17 m

23

44.8 m

24

17.6 m

25

24.6 m

30 m

26

57.2 m

30 m

27

46.8 m

28

20.1 m

29

53.5 m

30

15.9 m

Stock catalog

Herringbone linen board

Excellent linen fabric with a subtle herringbone pattern.