Biographer Jens Andersen locates a range of influences and inspiration for Pippi not only within educational theories of the 1930s, such as those of A. S. Neill and Bertrand Russell, but also contemporary films and comics that featured "preternaturally strong characters" (e.g. Superman and Tarzan). Literary inspiration for the character can be found in Alice's Adventures in Wonderland, E. T. A. Hoffmann's The Strange Child, Anne of Green Gables, and Daddy Long Legs in addition to myths, fairytales, and legends. Andersen argues that the "misanthropic, emotionally stunted age" of the Second World War, during which Lindgren was developing the character, provided the most influence: the original version of Pippi, according to Andersen, "was a cheerful pacifist whose answer to the brutality and evil of war was goodness, generosity, and good humor."
Pippi originates from bedside stories told for Lindgren's daughter, Karin. In the winter of 1941, Karin had come down with an illness and was confined to her sickbed; inspired by Karin's request to tell her stories about Pippi Longstocking, a name Karin created, Lindgren improvised stories about an "anything-but-pious" girl with "boundless energy." As a child, Karin related more to Annika and Tommy, rather than Pippi, whom she felt was very different from her personality. Pippi became a staple within the household, with Karin's friends and cousins also enjoying her adventures. In April 1944, while recovering from a twisted ankle, Lindgren wrote her stories about Pippi in shorthand, a method she used throughout her writing career; a copy of the clean manuscript was turned into a homemade book for Karin and gifted to her on May 21, while another was posted to publisher Bonnier Förlag, where it was rejected in September on the grounds of being "too advanced."
After her critical success with her debut children's novel The Confidences of Britt-Mari (1944), Lindgren sent the manuscript for Pippi Longstocking to her editor at Rabén and Sjögren, the children's librarian and critic Elsa Olenius, in May 1945. Olenius advised her to revise some of the "graphic" elements, such as a full chamber pot being used as a fire extinguisher, and then to enter it into the upcoming competition at Rabén and Sjögren, which was for books targeted at children between the ages of six and ten. Critic Ulla Lundqvist estimates that a third of the manuscript was altered, with some changes made to improve its prose and readability, and others done to the character of Pippi, who according to Lundqvist "acquire[d] a new modesty and tenderness, and also a slight touch of melancholy," as well as "less intricate" dialogue. Pippi Longstocking placed first and was subsequently published in November 1945 with illustrations by Ingrid Vang Nyman. Two more books followed: Pippi Goes on Board (1946) and Pippi in the South Seas (1948). Three picture books were also produced: Pippi's After Christmas Party (1950), Pippi on the Run (1971), and Pippi Longstocking in the Park (2001).